我试图获得一些最初使用GCC编译与MSVC编译的代码,并且在代码中遇到了回调包装类的问题。我已经提取下面的代码的关键部分:MSVC抱怨函数指针不是编译时常量
template <typename T_func>
struct internal_parameter_resolver;
template <typename R>
struct internal_parameter_resolver<R()> {
typedef R(*type)();
};
template <typename R, typename P1>
struct internal_parameter_resolver<R(P1)> {
typedef R(*type)(P1);
};
template <typename T_func, typename internal_parameter_resolver<T_func>::type func>
void bind() {
// Create and return instance of class Callback...
}
double func1() { return 0.5; }
int func2(double i) { return 0; }
int main() {
bind<double(), &func1>(); // (Line 23)
bind<int(double), &func2>(); // (Line 24)
return 0;
}
虽然这编译GCC下细,MSVC 2010提供了以下错误信息:
1>c:\users\public\documents\projects\_test\_test\main.cpp(23): error C2975: 'func' : invalid template argument for 'bind', expected compile-time constant expression
1> c:\users\public\documents\projects\_test\_test\main.cpp(14) : see declaration of 'func'
1>c:\users\public\documents\projects\_test\_test\main.cpp(24): error C2975: 'func' : invalid template argument for 'bind', expected compile-time constant expression
1> c:\users\public\documents\projects\_test\_test\main.cpp(14) : see declaration of 'func'
有谁知道为什么MSVC认为一个想法那些函数指针不是编译时常量?或者是代码中其他地方的问题(即不是第23行和第24行)?如果这是编译器中的错误,我会欢迎任何有关可能的解决方法的建议。
谢谢!
使用gcc 4.6 &&铿锵3.1编译正常。 – mfontanini 2012-08-11 21:24:07
@mfontanini是的,我知道,这是MSVC给我的问题。 – zennehoy 2012-08-11 21:25:10