c++ - 错误 C2784 : Could not deduce template argument

标签 c++ templates

仍在与模板作斗争。在这个例子中,尽管是直接从一本书中复制的,但我收到了以下错误消息:Error 2 error C2784: 'IsClassT<T>::One IsClassT<T>::test(int C::* )' : could not deduce template argument for 'int C::* ' from 'int'.

这是一本书中的例子 Templates - The Complete Guide . (我使用 Visual Studio 2010 RC)。

  template<typename T> 
    class IsClassT { 
      private: 
        typedef char One; 
        typedef struct { char a[2]; } Two; 
        template<typename C> static One test(int C::*); 
        template<typename C> static Two test(…); 
      public: 
        enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1 }; 
        enum { No = !Yes }; 
    }; 

class MyClass { 
}; 

struct MyStruct { 
}; 

union MyUnion { 
}; 

void myfunc() 
{ 
} 

enum E {e1} e; 

// check by passing type as template argument 
template <typename T> 
void check() 
{ 
    if (IsClassT<T>::Yes) { 
        std::cout << " IsClassT " << std::endl; 
    } 
    else { 
        std::cout << " !IsClassT " << std::endl; 
    } 
} 

// check by passing type as function call argument 
template <typename T> 
void checkT (T) 
{ 
    check<T>(); 
} 

int main() 
{ 
    /*std::cout << "int: "; 
    check<int>(); */

    std::cout << "MyClass: "; 
    check<MyClass>(); 
}

虽然我大致知道这个例子中发生了什么,但我无法修复这个错误。
感谢您的帮助。

最佳答案

如果您没有完全限定 test 表达式,我的编译器 (MSVC2008TS) 会喜欢它:

enum { Yes = sizeof(test<T>(0)) == 1 }; 

但这甚至是合法的代码吗?

关于c++ - 错误 C2784 : Could not deduce template argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2454817/

相关文章:

c++ - 如何直接在 Rcpp 包中调用 nloptr 包中的 C 函数?

c++ - 由空指针常量 : which behaviour is correct? 初始化

c++ - 使用没有模板的模板化类

c++ - 不同类型之间的模板类分配

c++ - 指向成员函数问题的指针

c++ - 库标题和#define

c++ - 如何在 2D 游戏中处理游戏事件长度

c++ - 如何使用其他类作为一个类的成员?

templates - Lua:将上下文传递到加载字符串中?

c++ - 如何根据元素数量终止可变参数模板递归?