简体   繁体   English

折叠表达式和 function 名称查找

[英]fold expression and function name lookup

I am learning fold expressions in C++17.我正在学习 C++17 中的折叠表达式。 I have the following code我有以下代码

#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 GCC 9.3.0 , the code compiles and runs correctly, but using Clang 10.0.0 , the same code does not compile:使用GCC 9.3.0 ,代码编译和运行正确,但使用Clang 10.0.0 ,相同的代码无法编译:

<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.

Why clang rejets the code but gcc accepts it?为什么 clang 拒绝代码但 gcc 接受它?

This was a Clang bug.这是一个 Clang 错误。 Clang versions 11 and earlier did not properly implement two-phase name lookup for the operator in a fold expression, and would incorrectly perform the first-phase lookup from the lexical scope in which the instantiation of the fold-expression happened to be performed rather than doing the first-phase lookup from the context of the template definition. Clang 版本 11 和更早版本没有正确实现折叠表达式中运算符的两阶段名称查找,并且会错误地从词汇 scope 执行第一阶段查找,其中折叠表达式的实例化恰好被执行而不是从模板定义的上下文中进行第一阶段查找。

I fixed this relatively recently (unfortunately not in time for the upcoming Clang 11 release), and the test case is now accepted by Clang trunk .我最近修复了这个问题(不幸的是,没有赶上即将发布的 Clang 11 版本),测试用例现在被 Clang trunk 接受

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM