簡體   English   中英

折疊表達式和 function 名稱查找

[英]fold expression and function name lookup

我正在學習 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';
}

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

為什么 clang 拒絕代碼但 gcc 接受它?

這是一個 Clang 錯誤。 Clang 版本 11 和更早版本沒有正確實現折疊表達式中運算符的兩階段名稱查找,並且會錯誤地從詞匯 scope 執行第一階段查找,其中折疊表達式的實例化恰好被執行而不是從模板定義的上下文中進行第一階段查找。

我最近修復了這個問題(不幸的是,沒有趕上即將發布的 Clang 11 版本),測試用例現在被 Clang trunk 接受

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM