类型特征检查 CRTP 派生,在基类中,问题是未定义的类型

Type Traits check OF CRTP Derived , in base class ,issue is undefined type

本文关键字:类型 问题是 未定义 基类 CRTP 派生 特征 检查      更新时间:2023-10-16

寻找像下面的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");
}

现场演示