I’m trying to get some code originally built with GCC to compile with MSVC, and am running into a problem with a callback wrapper class in the code. I’ve extracted the critical part of the code below:
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;
}
While this compiles fine under GCC, MSVC 2010 gives the following error message:
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'
Does anybody have an idea why MSVC thinks that those function pointers are not compile-time constants? Or is the problem elsewhere in the code (i.e., not lines 23 and 24)? If it’s a bug in the compiler, I would welcome any suggestions as to a possible workaround.
Thanks!
Visual C++ is ungood at resolving indirect type definitions, but it’s happy with more concrete types, like above.
The above shows what to do about the direct problem you have, with Visual C++.
However, a slightly better design would be to use automatic template argument deduction:
You can obtain the function result type from
std::function, if you want that, and so on.