c++ 折叠表达式和函数名查找[重复]

zf2sa74q  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(132)

此问题在此处已有答案

Clang can't find template binary operator in fold expression(3个答案)
2天前关闭。
我正在学习C++17中的折叠表达式。

  1. #include <iostream>
  2. #include <vector>
  3. namespace io {
  4. template<typename T>
  5. std::istream &operator>>(std::istream &in, std::vector<T> &vec) {
  6. for (auto &x : vec)
  7. in >> x;
  8. return in;
  9. }
  10. template<class... Args> void scan(Args &... args) {
  11. (std::cin >> ... >> args);
  12. }
  13. }// namespace io
  14. int main() {
  15. std::vector<int> s(1), t(1);
  16. io::scan(s, t);
  17. std::cout << s[0] << ' ' << t[0] << '\n';
  18. }

字符串
使用GCC 9.3.0,代码可以正确编译和运行,但使用Clang 10.0.0,相同的代码无法编译:

  1. <source>:13:16: error: call to function 'operator>>' that is neither visible in the template definition nor found by argument-dependent lookup
  2. (std::cin >> ... >> args);
  3. ^
  4. <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
  5. io::scan(s, t);
  6. ^
  7. <source>:6:15: note: 'operator>>' should be declared prior to the call site
  8. std::istream &operator>>(std::istream &in, std::vector<T> &vec) {
  9. ^
  10. 1 error generated.


为什么clang拒绝代码,但gcc接受它?

mkh04yzy

mkh04yzy1#

这是一个Clang bug。Clang版本11和更早的版本没有正确地实现折叠表达式中运算符的两阶段名称查找,并且会错误地从执行折叠表达式示例化的词法作用域执行第一阶段查找,而不是从模板定义的上下文执行第一阶段查找。
fixed this相对最近(不幸的是,没有及时为即将到来的Clang 11版本),测试用例现在是accepted by Clang trunk

相关问题