以下C++代码可以为GNU g++,LLVM和我抛出的所有其他C++编译器正确编译并运行,Microsoft VC6和VC7除外:

template<typename A, typename B> int HasVoidReturnType(A(*)(B)) { return 0; }
template<typename B> int HasVoidReturnType(void(*)(B)) { return 1; }
void f(double) {}
int foo() { return HasVoidReturnType(f); }

对于VC6和VC7,它无法编译并给出错误:
f.cpp(4) : error C2667: 'HasVoidReturnType' : none of 2 overloads have a best conversion
    f.cpp(2): could be 'int HasVoidReturnType(void (__cdecl *)(B))'
    f.cpp(1): or       'int HasVoidReturnType(A (__cdecl *)(B))'
    while trying to match the argument list '(overloaded-function)'
f.cpp(4) : error C2668: 'HasVoidReturnType' : ambiguous call to overloaded function
    f.cpp(2): could be 'int HasVoidReturnType(void (__cdecl *)(B))'
    f.cpp(1): or       'int HasVoidReturnType(A (__cdecl *)(B))'
    while trying to match the argument list '(overloaded-function)'

而不是争论哪种编译器是正确的优点,我如何使用VC6和VC7从模板函数确定函数是否具有空返回类型?

最佳答案

就VC++ 6而言,您不知所措,因为它不支持部分模板特化,这是解决此问题所需的工具。

关于c++ - 需要确定VC6和VC7中函数是否具有空返回类型的方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/524369/

10-10 13:56