C++ 铿锵++;二维数组指针错误
下面的代码使用g++可以很好地编译,但在c++11模式下使用clang++时失败。我注意到了内联错误。有人能确切地解释问题是什么吗C++ 铿锵++;二维数组指针错误,c++,c++11,lvalue,rvalue,C++,C++11,Lvalue,Rvalue,下面的代码使用g++可以很好地编译,但在c++11模式下使用clang++时失败。我注意到了内联错误。有人能确切地解释问题是什么吗 #include <iostream> using namespace std; int main(){ int nx=1000; int ny=1000; double *p=new double[nx*ny]; auto pp=(double (*)[nx])p;//fine auto pp2=(double
#include <iostream>
using namespace std;
int main(){
int nx=1000;
int ny=1000;
double *p=new double[nx*ny];
auto pp=(double (*)[nx])p;//fine
auto pp2=(double (*)[nx])p;//fine
pp=pp2; //error: assigning to 'double (*)[nx]' from incompatible type 'double (*)[nx]'
decltype(pp2) pp3=pp2;//fine
double (*pp4)[nx]=(double(*)[nx])p;//error: cannot initialize a variable of type 'double (*)[nx]' with an rvalue of type 'double (*)[nx]'
double (*pp5)[nx]=pp;//error: cannot initialize a variable of type 'double (*)[nx]' with an lvalue of type 'double (*)[nx]'
pp=pp2;//error: assigning to 'double (*)[nx]' from incompatible type 'double (*)[nx]'
pp=(double(*)[nx])p;//error: assigning to 'double (*)[nx]' from incompatible type 'double (*)[nx]'
pp[1][0]=1;
cout << pp2[1][0]<< endl;
}
#包括
使用名称空间std;
int main(){
int nx=1000;
int ny=1000;
双精度*p=新双精度[nx*ny];
自动pp=(双(*)[nx])p;//很好
自动pp2=(双(*)[nx])p;//很好
pp=pp2;//错误:从不兼容的类型“double(*)[nx]”分配给“double(*)[nx]”
decltype(pp2)pp3=pp2;//很好
double(*pp4)[nx]=(double(*)[nx])p;//错误:无法使用类型为“double(*)[nx]”的右值初始化类型为“double(*)[nx]”的变量
double(*pp5)[nx]=pp;//错误:无法使用类型为“double(*)[nx]'的左值初始化类型为“double(*)[nx]'的变量
pp=pp2;//错误:从不兼容的类型“double(*)[nx]”分配给“double(*)[nx]”
pp=(double(*)[nx])p;//错误:从不兼容的类型“double(*)[nx]”分配给“double(*)[nx]”
pp[1][0]=1;
这是一个gcc扩展:可变长度数组(也出现在C99中)。编译时带有警告和-pedantic
和-std=c++11
//fine
将是这两个初始DECL的相对术语(pp
和pp2
)因为他们都希望C++实现支持非标准可变长度数组,而CLAN至少应该警告你除了说出你正在发布的错误之外,还说“我的3.5个工具链当然是这样”。我在MAC 10.10中使用CLAN3.5Svn。你知道为什么它需要一些行而不是其他行吗?