isAbstractテンプレートとビジュアルスタジオ
-
03-07-2019 - |
質問
次のテンプレートは、Tがg ++で抽象的であるかどうかを決定します。
/**
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
クラスが追加されたことを報告TR1の一部としてのVS2008以降(ヘッダー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)() );
and
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 );
and
// Match if I can make a U2
template <typename U> static No test( U2<T> x );
運が悪い-すべての人は、テンプレート引数をUについて推論できないと言っています。
解決
これはVC9で機能します:
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( test<T>( 0 ) ) == sizeof(Yes) };
};
isAbstract<T>::
の呼び出しからtest
を削除する必要があることに注意してください。
他のヒント
しばらくの間C ++を実行したことに気付いたが、使用できるかどうかはわからない
template< typename T, typename U>
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) };
};
bool twoAbstract = isAbstract<myClass2, SomeTypeU>::result;
所属していません StackOverflow