类型特征检查 CRTP 派生,在基类中,问题是未定义的类型
Type Traits check OF CRTP Derived , in base class ,issue is undefined type
寻找像下面的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");
}
现场演示
相关文章:
- ";结果类型必须是可从输入范围的值类型""构造的;创建std::vector时
- 检查某些类型是否是模板类 std::optional 的实例化
- 输出错误,问题是找到总和5000位数字cpp
- c++无值sort()的问题是什么?
- 我的问题是关于C++中数字和序列的重复以及如何打印它们?
- 为什么协程的返回类型必须是可移动构造的?
- 什么是钻石问题?是一系列问题还是特定问题?
- C++ 未初始化的本地(非全局)int 数组中的元素类型到底是什么?
- 通过构造函数方法输出的类到类类型转换是 5500 为什么不是 5555
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 什么是非营利组织???我的问题是我不明白为什么有人会使用它
- T*&返回类型到底是什么
- 问题是找到最大的负数,最小的正数
- CPP:定义模板时,通用数据类型 T 是类吗?
- SFINAE - 检测类型 T 是指针、数组还是带有随机访问运算符的容器,以及给定的值类型
- 成员引用基类型不是结构或联合
- 模板冲突的类型-但类型应该是相同的cfr类层次结构
- 检查一组类型是否是具有组合的子集
- 类型特征检查 CRTP 派生,在基类中,问题是未定义的类型
- 我的问题是关于类成员函数作为类指针的返回类型