以下代码允许我更改*p2处的值,即使p2是用const声明的.
int *p1;
const decltype(p1) p2 = new int(100);
*p2 = 200;
cout << "*p2: " << *p2 << endl; // Outputs *p2: 200
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用"int*"而不是"decltype(p1)",则编译器会标记错误.
const int * p2 = new int(100);
*p2 = 200;
cout << "*p2: " << *p2 << endl;
error: assignment of read-only location ‘* p2’
*p2 = 200;
^
Run Code Online (Sandbox Code Playgroud)
我正在使用g ++(Ubuntu 4.8.2-19ubuntu1)4.8.2.
当应用于指针变量时,decltype是否忽略const说明符?