正在寻找像下面的 EvalDelay 这样的解决方案来修复未定义的类型问题 EvalDelay是我尝试解决的问题,但是没有效果
由于在派生的基类中检查了特征,派生仍然是未定义的 问题是我如何使用一些模板魔术来延迟评估
特征检查在这里保持简单,它只是检查的基础。
struct Base{};
template<class T_Type>
struct T_CheckTrait
{
static const bool bVal = std::is_base_of_v<Base, T_Type>;
};
template<class TypeToDelay, class T = Next>
struct EvalDelay
{
//using type = std::add_volatile<TypeToDelay>;
//using type = typename type_identity<TypeToDelay>::type;
using type = TypeToDelay;
};
template<class T_Derived>
struct RexBase
{
using T_TypeDly = typename EvalDelay<T_Derived>::type;
static const bool bVal = T_CheckTrait<T_TypeDly>::bVal;
};
struct Rex:RexBase<Rex>{ };
void Main
{
Rex Obj; //and on compilation i get error undefined type, not here but in templates above
}
不编译是因为我试图在编译时检查其基类中 Rex 的特征。
寻找模板魔术来延迟评估
std::add_volatile 确实延迟求值,如 EvalDelay 中所示,但它将它延迟到运行时,寻找编译时求值但延迟了。
谢谢
最佳答案
不确定您的最终目标是什么,但以下是延迟类型特征评估的方法:
#include <type_traits>
struct Base {};
template<class T>
struct EvalDelay
{
using type = T;
};
template<class T_Derived>
struct RexBase
{
using is_base = typename EvalDelay<std::is_base_of<Base, T_Derived>>::type;
};
struct Rex : RexBase<Rex> { };
struct Rex2 : RexBase<Rex2>, Base { };
int main()
{
Rex Obj;
static_assert(Rex::is_base::value == false, "Rex is not Base");
static_assert(Rex2::is_base::value == true, "Rex2 is Base");
}
关于c++ - 类型特征检查 OF CRTP 派生,在基类中,问题是未定义的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55538857/