Consider the following code:
#include <iostream>
#include <vector>
#include <array>
using namespace std;
typedef double (C_array)[10];
int main()
{
std::vector<C_array> arr(10);
// let's initialize it
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
arr[i][j] = -1;
// now make sure we did the right thing
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
}
I just found out from @juanchopanza https://stackoverflow.com/a/25108679/3093378 that this code should not be legal, since a plain old C
-style array is not assignable/copyable/movable. However g++
flies through the code, even with -Wall -Wextra -pedantic
. clang++
doesn't compile it. Of course if I try to do something like auto arr1 = arr;
, it fails under g++
, as it doesn't know how to copy arr
into arr1
.
I used g++4.9
from macports
under OS X Mavericks.
Live code here: http://goo.gl/97koLa
My questions are:
- Is this code illegal according to the standard?
- Is
g++
so buggy? I keep finding lots of simple examples in whichg++
blindly compiles illegal code, last was yesterday user-defined conversion operators precedence, compiles in g++ but not clang++ , and without too much effort, just experimenting withC++
for fun.