gpt4 book ai didi

c++ - 在这种情况下,关键字 "typename"和 "template"都不需要吗?

转载 作者:行者123 更新时间:2023-12-04 11:12:21 24 4
gpt4 key购买 nike

template<class T> 
T::type<int> f(){

}
根据 [temp.names#3.4]

A < is interpreted as the delimiter of a template-argument-list if it follows a name that is not a conversion-function-id and

  • [...]
  • that is a terminal name in a using-declarator ([namespace.udecl]), in a declarator-id ([dcl.meaning]), or in a type-only context other than a nested-name-specifier ([temp.res]).


根据 [temp.res#general-4.3.1], T::type<int>由于以下规则,确实满足上述规则(强调我的)

A qualified or unqualified name is said to be in a type-only context if it is the terminal name of

  • [...]
  • a decl-specifier of the decl-specifier-seq of a
  • [...]
  • simple-declaration or a function-definition in namespace scope,

T::type<int> 的声明说明符功能定义用于模板函数 f这是在命名空间范围内,因此终端名称 type据说是在仅类型上下文中。
另外,根据 [temp.res#general-5]

A qualified-id whose terminal name is dependent and that is in a type-only context is considered to denote a type.


因此,符号 <T::type<int>由于 [temp.names#3.4] 而限定 ID T::type<int> 被解释为模板参数列表的分隔符由于 [temp.res#general-5] 被认为表示一种类型,该示例应该是合法的。但是,被 Clang and GCC都拒绝了。 .
我想知道,都是关键字 typenametemplate在这个例子中不需要由 future 的实现编译?

最佳答案

是的,这是规则,它是 正确 ;编译器根本没有实现(更新的)template部分。在讨论这个添加时,提出了一个例子,说明在这种情况下要求关键字是荒谬的:

template<typename T> struct A {
  template<typename U> struct B {
    B();
  };
  template<typename U> B<U> make();
};
template<typename T> template<typename U>
A<T>::B<U>::B() {} // no 'template' keyword required before 'B' here, but...
template<typename T> template<typename U>
A<T>::B<U> A<T>::make() { return {}; } // 'template' keyword required before 'B' here?
这也说明了放弃对 typename 的要求的部分动机。在许多情况下。 A<T>::B可能是一个从属名称(如果声明最终用于不是(的主模板) A 的成员的东西),但这不会干扰解析它,因为没有 表达式 可以出现在那里。

关于c++ - 在这种情况下,关键字 "typename"和 "template"都不需要吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68633295/

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