仍在与模板作斗争。在这个例子中,尽管是直接从一本书中复制的,但我收到了以下错误消息: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/