gpt4 book ai didi

c++ - 类模板,在定义中引用它自己的类型

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:57 24 4
gpt4 key购买 nike

我想知道关于标准的以下情况,visual studio 2017 和 GCC 哪个是正确的。问题是在类模板 second 中,visual studio 中的标识符“second”总是指具体类型,但在 gcc 中它似乎是上下文相关的。

GCC 示例

template <typename...>
struct type_list{};

template<template <typename...> typename tmpl>
struct tmpl_c
{
template <typename...Ts> using type = tmpl<Ts...>;
};

template<typename> struct template_of;
template <template <typename...> typename tmpl, typename... Ts>
struct template_of<tmpl<Ts...>>{
using type = tmpl_c<tmpl>;

};


template <typename T>
struct first{};


template <typename T>
struct second
{
// 'second' here refers to second<int>
using test = second;

// 'second' here refers to the template second
// is this due to the context? ie that the tmpl_c is expecting a template not a concrete type?
using types = type_list<tmpl_c<first>, tmpl_c<second> >;


// second here refers to the concrete type 'second<int>'
// this workaround is needed for visual studio as it seems second always
// refers to the concrete type, not the template.
using types2 = type_list<tmpl_c<first>, typename template_of<second>::type >;
};

Demo

和 Visual Studio 示例(只有不同​​的部分)

template <typename T>
struct second
{
// 'second' here refers to second<int>
using test = second;

// 'second' here refers to second<int>
// this doesn't compile in visual studio as second<int> not the template.
//using types = type_list<tmpl_c<first>, tmpl_c<second> >;


// second here refers to the concrete type 'second<int>'
// this workaround is needed for visual studio as it seems second always
// refers to the concrete type, not the template.
using types2 = type_list<tmpl_c<first>, typename template_of<second>::type >;
};

Demo

由于 template_of 解决方法似乎在两者中都能正常运行,这是我目前唯一的选择..但我仍然想知道哪个是正确的,或者是否有其他解决方法..

fixed in visual studio 15.8 Preview 2

最佳答案

Gcc 是正确的。根据injected-class-name for class template的用法规则,注入(inject)的类名也可以用作模板名或类型名。

(强调我的)

Like other classes, class templates have an injected-class-name. The injected-class-name can be used as a template-name or a type-name.

In the following cases, the injected-class-name is treated as a template-name of the class template itself:

  • it is followed by <
  • it is used as a template argument that corresponds to a template template parameter
  • it is the final identifier in the elaborated class specifier of a friend class template declaration.

Otherwise, it is treated as a type-name, and is equivalent to the template-name followed by the template-parameters of the class template enclosed in <>.

也就是说,

template <typename T>
struct second
{

// second is same as second<T>
using test = second;

// second is considered as a template-name
// it's used as template argument for a template template parameter
using types = type_list<tmpl_c<first>, tmpl_c<second> >;

// second is same as second<T>
using types2 = type_list<tmpl_c<first>, typename template_of<second>::type >;
};

关于c++ - 类模板,在定义中引用它自己的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51033338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com