I get weird behavioral estimates by checking the std :: array element with mingw (gcc 4.7.0) with the following code
#include <iostream> #include <array> class testClass { std::array<int, 2> testArray; public: testClass(); void func() const; }; testClass::testClass() : testArray({{1, 2}}) { } void testClass::func() const { for (int i = 0; i < 2; ++i) std::cout << testArray.at(i) << '\n' << testArray[i] << '\n'; } int main() { testClass test; test.func(); }
Exit
0 1 0 2
The error seems to be related to optimization, since it only occurs when compiling with -O , I tried the individual flags allowed by -O , but could not narrow it further. A problem with the non-const function also fixes the problem. Could this be a mistake, or am I missing something?
* change
Narrowed down, looks like an error in the const .at() version
#include <iostream> #include <array> int main() { std::array<int, 2> const testArray = {1, 2}; for (int i = 0; i < 2; ++i) std::cout << testArray.at(i) << '\n' << testArray[i] << '\n'; }
The same output as above was compiled using -std=c++11 -O using mingw 4.7.0 for Windows Xp sp3 and Windows 7 sp1.
* edit 2
Repeat the same conclusion
#include <iostream> #include <array> int main() { typedef std::array<int, 2> Tarray; Tarray test = {1, 2}; for (int i = 0; i < 2; ++i) std::cout << const_cast<Tarray const*>(&test)->at(i) << '\n' << test.at(i) << '\n'; }
c ++ c ++ 11 mingw
user657267
source share