次のコードを使用して、mingw (gcc 4.7.0) でメンバー std::array をチェックすると、奇妙な動作境界が発生します。
#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();
}
出力は
0
1
0
2
このエラーは最適化に関連しているよう-O
です-O
. 関数を非 const にすると、問題も修正されます。これはバグでしょうか、それとも何か不足していますか?
*編集
絞り込むと、のconst
バージョンのバグのように見えます.at()
#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';
}
-std=c++11 -O
Windows Xp sp3 および Windows 7 sp1 で mingw 4.7.0を使用してコンパイルされた上記と同じ出力。
*編集2
再び同じ出力
#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';
}