[basic.lookup.unqual]/3 中的第一个示例
First example in [basic.lookup.unqual]/3
[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
上面的注释似乎表明编译器应该以不同的方式对待对上述g
和h
名称的查找,就好像名称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 版本不包含您提到的示例。
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- OpenGL 2D游戏只绘制第二个精灵纹理而不是第一个
- C++ 为什么程序只读取第一个值
- 在我的第一个C++程序中需要一些帮助(简单)
- 为什么我的代码在第一个 if 语句处中断?
- 是否可以从另一个类对象调用一个类函数而不继承第一个类
- 无法使我的第一个Windows OpenGL窗口抬起并运行
- 将参数初始化为构造函数,而不是第一个
- 无法在硬件模式下创建 SGX 安全区 - "invalid launch token"即使文档将无效的启动令牌指定为第一个