문제

다음 템플릿은 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). 그러나 설치에서 누락 된 것 같습니다.

추신. 길고 지루한 이유로, 나는 이것을 부스트를 통해 상환 할 수 없습니다.

업데이트

또한 교체 시도,

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에 대해 추론 될 수 없다고 말한다.

도움이 되었습니까?

해결책

이것은 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;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top