Why does GCC optimization not work with valarrays?
问题 This is a simple c++ program using valarrays: #include <iostream> #include <valarray> int main() { using ratios_t = std::valarray<float>; ratios_t a{0.5, 1, 2}; const auto& res ( ratios_t::value_type(256) / a ); for(const auto& r : ratios_t{res}) std::cout << r << " " << std::endl; return 0; } If I compile and run it like this: g++ -O0 main.cpp && ./a.out The output is as expected: 512 256 128 However, if I compile and run it like this: g++ -O3 main.cpp && ./a.out The output is: 0 0 0 Same