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

标签 c++ templates lazy-evaluation traits crtp

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

Live demo

关于c++ - 类型特征检查 OF CRTP 派生,在基类中,问题是未定义的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55538857/

相关文章:

c# - 是什么使模板与通用模板不同?

c++ - 使用 operator() 将类型名称作为参数传递

haskell - 为什么引入严格性的函数称为 seq?

haskell - 读取文件后 Haskell 文件会自动关闭吗?

c++ - 不完整类型的 std::unique_ptr 无法编译

c++ - Unresolved external symbol (已经阅读过类似的问题,但没有运气)

c++ - valgrind memcheck 给出消息 "in a rw- anonymous segment"

c++ - 奇怪的重复模板模式——继承和 friend

c# - linq max惰性求值

c++ - 创建回合制游戏以及如何获得实际减去的值?我每次攻击都能得到 100 的返回?