C++11 独特的含义\u ptr<;常数T>;

C++11 独特的含义\u ptr<;常数T>;,c++11,C++11,为什么unique_ptr可以获得const ptr的所有权 unique\u ptr的含义是什么 std::unique_ptr a(新int); //只有这是编译错误。 std::unique_ptr b((const int*)new int); std::唯一的ptr c(新int); //为什么unique_ptr可以接受常量ptr,并在解构时将其删除? std::unique_ptr d((const int*)new int); 使用g++4.8进行测试 为什么unique_pt

为什么
unique_ptr
可以获得
const ptr
的所有权

unique\u ptr
的含义是什么

std::unique_ptr a(新int);
//只有这是编译错误。
std::unique_ptr b((const int*)new int);
std::唯一的ptr c(新int);
//为什么unique_ptr可以接受常量ptr,并在解构时将其删除?
std::unique_ptr d((const int*)new int);
使用g++4.8进行测试

为什么unique_ptr可以获得const ptr的所有权

因为即使是常量变量也需要在不再需要的时候进行解构和释放。无论
const
volatile
关键字如何,未最终确定的变量都会发生内存和资源泄漏

独特的含义是什么

uniqe\u ptr
具有的任何含义,除了它是
const T
而不是
T

//为什么unique_ptr可以获取常量ptr,并在 解构


我有一种感觉,您认为
const
对象不能删除,因为它们是
const
对象,因此不可修改且不可销毁。这是错误的。
const
在构造变量并“停止”影响变量销毁后生效。否则,您无法创建任何
const
变量,也无法销毁它们。

const int*
的含义相同suppose@Casey隐形编辑。。。原来是
int*const
  std::unique_ptr<int> a(new int);
  // Only this is compile error.                                 
  std::unique_ptr<int> b((const int*)new int);
  std::unique_ptr<const int> c(new int);
  // Why unique_ptr can take a const ptr, and delete it when deconstruct?
  std::unique_ptr<const int> d((const int*)new int);