我將如何進行相當于以下的作業?
我將如何進行相當于以下的作業?
template < class T, typename = std: :enable_if< std::is_base_of< Self, T >:value > > // Can't use std::is_base_of on self
class Self {
protected:
typedef T self;
};
class ValidDerived : public Self< ValidDerived > { }; // this should compile because T is itself.
class InvalidDerived : public Self< ValidDerived > { }; // This should not compile because T is not itself.
我正在嘗試實作反射,要做到這一點,我必須要做的一個步驟是獲得最派生類的typeid( self ).name()。
uj5u.com熱心網友回復:
在CRTP中,T在class MyClass : Self<MyClass> {};中不完整。
你可以在一個應該被呼叫/實體化的方法(如建構式/解構函式)中添加一個額外的檢查:
template<class T>
class Self Self?
{
protected:
using self = T;
Self() { static_assert(std::is_base_of<Self, T > ::value); }; }; }
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/320093.html
標籤:
