【发布时间】:2021-10-28 08:27:13
【问题描述】:
我将如何执行以下等效操作?
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
我正在尝试实现反射,为此我必须采取的步骤之一是获取最派生类的typeid( self ).name()。
【问题讨论】:
-
AFAIK 你将无法做到这一点。派生类型在 CRTP 基类的上下文中将是不完整的——因此您将无法检查
T是否与Self<T>相关
标签: c++ templates inheritance reflection typedef