问题描述
限时送ChatGPT账号..我正在尝试编写一个类型特征来检测一个类型是否具有某种类型的 T::type
.我正在使用此答案中的代码.作为参考,这是我正在使用的代码的一部分:
//参见 http://www.open-std/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf.模板 <typename...>使用 void_t = void;//主模板处理所有不支持操作的类型.模板<类型名,模板<类型名>类,类型名称 = void_t<>>结构检测:std::false_type {};//专业化仅识别/验证支持原型的类型.模板<类型名T,模板<类型名>类操作>结构检测>: std::true_type {};
我从一个简单的特征开始检测 T::type
:
template 使用 has_type_t = typename T::type;模板 使用 has_type = detect;
这按预期工作,但是当我询问 T::type
的实际类型时,我也收到了我不明白的错误:
template 结构 has_X_type_helper {模板 using type = typename std::enable_if_t<std::is_same_v<typename T::type, X>,int>;};模板 使用 has_X_type = detect::type>;
海湾合作委员会:
而 Clang 对我来说更令人困惑
has_X_type_helper
不是类型别名模板吗?我的代码有什么问题?
@godbolt
解决方案需要说明嵌套的东西是模板:
template 使用 has_X_type = detect<T, has_X_type_helper<X>::template type>;//~~~~~~~~~^
I am trying to write a type trait to detect if a type has a T::type
of certain type. I am using code from this answer. For reference this is the part of the code I am using:
// See http://www.open-std/jtc1/sc22/wg21/docs/papers/2015/n4502.pdf. template <typename...> using void_t = void; // Primary template handles all types not supporting the operation. template <typename, template <typename> class, typename = void_t<>> struct detect : std::false_type {}; // Specialization recognizes/validates only types supporting the archetype. template <typename T, template <typename> class Op> struct detect<T, Op, void_t<Op<T>>> : std::true_type {};
I started simple with a trait to detect the T::type
:
template <typename T>
using has_type_t = typename T::type;
template <typename T>
using has_type = detect<T, has_type_t>;
This works as expected, but when I ask for the actual type of T::type
also I get errors that I do not understand:
template <typename X>
struct has_X_type_helper {
template <typename T>
using type = typename std::enable_if_t<std::is_same_v< typename T::type, X>,int>;
};
template <typename T,typename X>
using has_X_type = detect<T,has_X_type_helper<X>::type>;
GCC:
<source>:49:55: error: type/value mismatch at argument 2 in template parameter list for 'template<class, template<class> class<template-parameter-1-2>, class> struct detect'
49 | using has_X_type = detect<T,has_X_type_helper<X>::type>;
| ^
<source>:49:55: note: expected a class template, got 'has_X_type_helper<X>::type'
and Clang is even more confusing for me
<source>:49:29: error: template argument for template template parameter must be a class template or type alias template
using has_X_type = detect<T,has_X_type_helper<X>::type>;
^
Is has_X_type_helper<X>::type
not a type alias template ? What is wrong in my code?
@ godbolt
解决方案You need to indicate that the nested thing is a template:
template <typename T, typename X>
using has_X_type = detect<T, has_X_type_helper<X>::template type>;
// ~~~~~~~~~^
这篇关于别名模板不是类模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论