以下模板将决定T是否是用g ++抽象的。isAbstract template and visual studio
/**
isAbstract<T>::result is 1 if T is abstract, 0 if otherwise.
*/
template<typename T>
class isAbstract
{
class No { };
class Yes { No no[3]; };
template<class U> static No test(U (*)[1]); // not defined
template<class U> static Yes test(...); // not defined
public:
enum { result = sizeof(isAbstract<T>::template test<T>(0)) == sizeof(Yes) };
};
例如: struct myClass2 {virtual void f(){}}; struct myClass1 {virtual void f()= 0; };
bool twoAbstract = isAbstract<myClass2>::result;
bool oneAbstract = isAbstract<myClass1>::result;
然而,在Visual Studio 9.0失败,出现错误:
error C2784: 'AiLive::isAbstract<T>::No AiLive::isAbstract<T>::test(U (*)[1])' : could not deduce template argument for 'U (*)[1]' from 'myClass2'
有没有人有问题是什么,如何解决这一问题的想法?
MSDN报告他们现在有一个is_abstract
级别,因为VS2008是TR1的一部分(在标头type_traits
内)。但是,它似乎从我的安装中丢失。
PS。由于长期无聊的原因,我无法通过Boost重新实现。
更新
另外,试图替换,
template<class U> static No test(U (*)[1]);
与每个,
template<class U> static No test(U (*x)[1]);
template<class U> static No test(U (*)());
template<class U> static No test(U (*x)());
和
template <typename U>
struct U2 : public U
{
U2(U*) {}
};
// Match if I can make a U2
template <typename U> static No test(U2<U> x );
和
// Match if I can make a U2
template <typename U> static No test(U2<T> x );
没有运气 - 都说模板参数不能推导出U.
我认为代码很好,怀疑VC++的错误。也许试试这个:template static没有测试(U(*)());或者参数的名称(可能是因为参数没有名字而导致窒息?)。 –
2009-05-01 00:08:44
谢谢。试过这些,但工作原理是一样的。 (我已经在上面添加了这些案例和一个额外的案例)。 – user48956 2009-05-01 01:26:48
嗯,只是看起来更深。我的建议U(*)()似乎不起作用,'因为具有抽象返回类型的函数类型不是无效的。这是不可能有这样的功能。导致一个“真正的”编译时间错误与comeau :) – 2009-05-04 13:09:00