考虑以下代码:
#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;
}
}
我刚刚从@juanchopanza https://stackoverflow.com/a/25108679/3093378发现这段代码不应该是合法的,因为普通的旧式C
数组是不可分配/可复制/可移动的。但是g++
,即使使用-Wall -Wextra -pedantic
. clang++
不编译它。当然,如果我尝试做类似的事情auto arr1 = arr;
,它会失败g++
,因为它不知道如何复制arr
到arr1
.
我在 OS X Mavericks 下使用g++4.9
过。macports
现场代码:http: //goo.gl/97koLa
我的问题是:
- 根据标准,此代码是否非法?
g++
这么牛逼吗?我一直在寻找很多简单的例子,其中g++
盲目地编译非法代码,最后是昨天 用户定义的转换运算符优先级,在 g++ 但不是 clang++ 编译,并且没有太多努力,只是C++
为了好玩而尝试。