boost :: function的奇怪行为与函数指针

时间:2011-08-16 16:33:19

标签: c++ boost

当我偶然发现以下问题时,我正在玩boost :: function。

typedef int(*IFnPtr2)(Interface*,int,bool&);
IFnPtr2 p = NULL;
boost::function<int(Interface*,int,bool&)> fn; // this is fine
boost::function<IFnPtr2> fn2; // this gives me a compile error

我想知道为什么函数在与type和typedef一起使用时应该表现不同的行为应该表示相同的类型。这对我来说不是问题,因为我只是不使用typedef,但我仍然很想知道为什么会有区别。
我使用的编译器是MSVC2010。

1 个答案:

答案 0 :(得分:6)

将模板参数的函数类型用于boost::function,而不是函数指针类型:

typedef int function_type(Interface*,int,bool&);
function_type* p = 0; // Pointer to function here
boost::function<function_type> fn;