我正在学习 C++17 中的折叠表达式。我有以下代码
#include <iostream>
#include <vector>
namespace io {
template<typename T>
std::istream &operator>>(std::istream &in, std::vector<T> &vec) {
for (auto &x : vec)
in >> x;
return in;
}
template<class... Args> void scan(Args &... args) {
(std::cin >> ... >> args);
}
}// namespace io
int main() {
std::vector<int> s(1), t(1);
io::scan(s, t);
std::cout << s[0] << ' ' << t[0] << '\n';
}
Using 海湾合作委员会9.3.0 https://wandbox.org/permlink/HxCd8zcnwIuq6bpR,代码编译并正确运行,但使用铿锵10.0.0 https://godbolt.org/z/wTMzhU,相同的代码无法编译:
<source>:13:16: error: call to function 'operator>>' that is neither visible in the template definition nor found by argument-dependent lookup
(std::cin >> ... >> args);
^
<source>:19:9: note: in instantiation of function template specialization 'io::scan<std::vector<int, std::allocator<int> >, std::vector<int, std::allocator<int> > >' requested here
io::scan(s, t);
^
<source>:6:15: note: 'operator>>' should be declared prior to the call site
std::istream &operator>>(std::istream &in, std::vector<T> &vec) {
^
1 error generated.
为什么 clang 拒绝代码但 gcc 接受它?
这是一个 Clang 错误。 Clang 版本 11 及更早版本没有正确实现折叠表达式中运算符的两阶段名称查找,并且会错误地从恰好执行折叠表达式实例化的词法范围执行第一阶段查找,而不是从模板定义的上下文中进行第一阶段查找。
I 修复了这个 https://github.com/llvm/llvm-project/commit/ed5a18fc0399dce994aa354a33e6f981f9828647相对最近(不幸的是,没有及时赶上即将发布的 Clang 11 版本),测试用例现在是被 Clang trunk 接受 https://godbolt.org/z/sboK3P.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)