gpt4 book ai didi

c++ - 不合格的名称查找

转载 作者:行者123 更新时间:2023-12-01 22:58:45 24 4
gpt4 key购买 nike

这是来自标准 (C++20) - 非限定名称查找 6.5.2。谁能解释一下这里发生了什么?注意:这不是 ADL。我正在专门寻找对这个简短句子的解释:“在某些情况下,名称后跟 < 被视为 template-name,即使名称查找没有找到 template-名字

int h;
void g();
namespace N{
struct A{};
template <class T> int f(T);
template <class T> int g(T);
template <class T> int h(T);
}
int x = f<N::A>(N::A()); // OK: lookup of f finds nothing; f treated as template name
int y = g<N::A>(N::A()); // OK: lookup of g finds a function, g treated as template name
int z = h<N::A>(N::A()); // error: h< does not begin a template-id

最佳答案

f<N::A>(N::A()) , <可以是小于运算符或启动模板参数列表。为了消除歧义,编译器需要查看是否 f (< 之前的名称)命名模板。

请注意,此时无法执行 ADL,因为当编译器看到 < 时,他们甚至不知道是否有争论。

C++20 指定如果通常的名称查找什么也找不到,f被视为一个模板名称。因此

  1. <有问题的被认为是启动模板参数列表,这意味着
  2. f<N::A>被解析为模板ID,这意味着
  3. f<N::A>(N::A())被解析为函数调用表达式,这意味着
  4. 执行ADL 看看是什么f真的是。

C++20之前,<之前的名字当且仅当通常的名称查找找到模板时,才会被视为模板名称,所以这些都不会发生,<被视为运营商,f<N::A>(N::A())被视为错误。

更改由 P0846R0 完成.

C++20 [temp.name]/2 :

An identifier is a template-name if it is associated by name lookup with a template or an overload set that contains a function template, or the identifier is followed by <, the template-id would form an unqualified-id, and name lookup either finds one or more functions or finds nothing.

C++20 [temp.names]/3 :

When a name is considered to be a template-name, and it is followed by a <, the < is always taken as the delimiter of a template-argument-list and never as the less-than operator.

最新草案可以说更清楚 ( [temp.names]/3 ):

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 an unqualified name for which name lookup either finds one or more functions or finds nothing, or
  • [...].

关于c++ - 不合格的名称查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72421646/

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