【发布时间】:2015-04-18 23:00:25
【问题描述】:
以下代码允许我更改 *p2 的值,即使 p2 是用 const 声明的。
int *p1;
const decltype(p1) p2 = new int(100);
*p2 = 200;
cout << "*p2: " << *p2 << endl; // Outputs *p2: 200
但是,如果我使用“int *”而不是“decltype(p1)”,那么编译器会标记错误。
const int * p2 = new int(100);
*p2 = 200;
cout << "*p2: " << *p2 << endl;
error: assignment of read-only location ‘* p2’
*p2 = 200;
^
我正在使用 g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2。
decltype 应用于指针变量时是否忽略 const 说明符?
【问题讨论】: