仅为派生的 classes 启用模板化基础 class
enable templated base class only for derived classes
我将如何着手做以下等同的事情?
template < class T, typename = std::enable_if< std::is_base_of< Self, T >::value > > // Can not 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
我正在尝试实施反射,为此我必须执行的步骤之一是获取最衍生的 class。
的 typeid( self ).name()
在 CRTP 中,T
在 class MyClass : Self<MyClass> {};
中不完整。
您可以在方法中添加额外的检查,应该是called/instantiated(例如constructor/destructor):
template<class T>
class Self
{
protected:
using self = T;
Self() { static_assert(std::is_base_of<Self, T >::value); }
};
我将如何着手做以下等同的事情?
template < class T, typename = std::enable_if< std::is_base_of< Self, T >::value > > // Can not 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
我正在尝试实施反射,为此我必须执行的步骤之一是获取最衍生的 class。
的typeid( self ).name()
在 CRTP 中,T
在 class MyClass : Self<MyClass> {};
中不完整。
您可以在方法中添加额外的检查,应该是called/instantiated(例如constructor/destructor):
template<class T>
class Self
{
protected:
using self = T;
Self() { static_assert(std::is_base_of<Self, T >::value); }
};