考虑下面的MCVE,其中有两个值数组,其中w两次是两次v(在此处尝试):
#include <valarray>
using namespace std;
int main() {
valarray<int> v { 1, 2, 3 };
for ([[maybe_unused]] auto x : v) {} // Ok
auto w = v * 2; // Leads to failure in loop below
//valarray<int> w = v * 2; // Works
//auto w = v*=2; // Works
//auto w = v; w *= 2; // Works
for ([[maybe_unused]] auto x : w) {} // Failure here
}
Run Code Online (Sandbox Code Playgroud)
这个例子在最后一个循环中用clang和gcc编译失败(这里是gcc输出):
error: …Run Code Online (Sandbox Code Playgroud)