小编Ahm*_*hid的帖子

将const和decltype与指针变量一起使用

以下代码允许我更改*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说明符?

c++ c++11

6
推荐指数
2
解决办法
416
查看次数

标签 统计

c++ ×1

c++11 ×1