[basic.lookup.unqual]/3 中的第一个示例

First example in [basic.lookup.unqual]/3

本文关键字:第一个 basic lookup unqual      更新时间:2023-10-16

[basic.lookup.unqual]/3 中的第一个示例:

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

上面的注释似乎表明编译器应该以不同的方式对待对上述gh名称的查找,就好像名称h不考虑模板 id 一样。当我使用 clang 或 GCC 编译此代码片段时,我看不到这种区别。这个例子试图传达的区别到底是什么?

您正在查看 C++20 草案,但使用 C++17 编译器进行测试。

非限定模板名称查找是 P0846R0 引入并被 C++20 草案采用的一项新的 ADL 功能。

因此,要尝试一下,请使用带有-std=c++2a的GCC中继(链接(:

error: expected primary-expression before '>' token
   12 | int z = h<N::A>(N::A());        // error: h< does not begin a template-id

鉴于确切的错误消息还不完美,最终结果是前两次查找成功。

为了进行比较,请注意 [basic.lookup.unqual]/3 的 C++17 版本不包含您提到的示例。