سؤال

وسوف القالب التالي تقرر ما إذا كان 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) }; 
};

وعلى سبيل المثال:     البنية myClass2 {افتراضية الفراغ و () {}}؛     myClass1 البنية {افتراضية الفراغ و () = 0؛ }؛

bool twoAbstract = isAbstract<myClass2>::result;
bool oneAbstract = isAbstract<myClass1>::result;

ومع ذلك، فإنه فشل في الاستوديو المرئي 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. لأسباب طويلة ومملة، لا أستطيع أن reimplement ذلك عن طريق تفعيل.

تحديث

وبالإضافة إلى ذلك، حاول استبدال،

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