别名模板不是类模板?

编程入门 行业动态 更新时间:2024-10-28 06:29:40
本文介绍了别名模板不是类模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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>;

海湾合作委员会:

:49:55: 错误:'template 的模板参数列表中参数 2 的类型/值不匹配class<template-parameter-1-2>, class>结构检测'49 |使用 has_X_type = detect::type>;|^<source>:49:55: 注意:需要一个类模板,得到 'has_X_type_helper<X>::type'

而 Clang 对我来说更令人困惑

:49:29: 错误:模板模板参数的模板参数必须是类模板或类型别名模板使用 has_X_type = detect::type>;^

has_X_type_helper::type 不是类型别名模板吗?我的代码有什么问题?

@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:关键词]

本文发布于:2023-04-29 09:11:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1187584.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模板   别名

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!