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

问题描述 投票:1回答:1

寻找像EvalDelay下面的解决方案来修复未定义的类型问题EvalDelay是我尝试解决问题,但没有工作

由于traits是在派生的基类中检查的,派生的仍然是未定义的问题是如何使用一些模板魔法延迟评估

这里的特质检查很简单,它只是一个检查的基础。

 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所示,但它将它延迟到运行时间,寻找编译时评估但延迟了。

谢谢

c++ templates lazy-evaluation traits crtp
1个回答
0
投票

不确定你的最终目标是什么,但这里是如何推迟对类型特征的评估:

#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");
}

Live demo

© www.soinside.com 2019 - 2024. All rights reserved.