下面是代码示例:std ::函数签名指针vs指针引用=没有区别?
#include <string>
#include <functional>
struct Foo {};
typedef bool func_type(Foo *&, const std::string&);
typedef std::function<bool(Foo*&, const std::string&)> FunctionalType;
bool f(Foo *, const std::string&)
{
}
int main()
{
#if 1
func_type *func;
func = f;
#else
FunctionalType f2;
f2 = f;
#endif
}
正如你看到的,我已经声明函数类型与“参考指针”作为第一个参数Foo *&
,我希望这个函数只有“指针”作为第一个参数Foo *
不能被分配给这种类型的变量。
#if 1
区域无法编译(如我所料);然而,替代没有发出任何错误:
FunctionalType f2;
f2 = f;
为什么它编译没有错误(至少GCC 5.2和3.7哗)?
如何修复,以便
std::function<Params>
不接受f
进行转换?
我将另一个答案标记为答案,因为它为(2)提供了一些解决方法。 – fghj