google abseil c++ tips[55] name counting and unique_ptr 变量名计数和unique_ptr

前置知识

  1. 左值 右值
  2. unique_prt

核心

这里暂且认为所有的name都对应一个左值.
比如

    int i=0;
    int&& ir=std::move(i);

i是一个左值 ir 的类型是右值引用,但是呢,它还是个左值.
大概可以理解成有名字的就是左值.

** Any non-null pointer value stored in a std::unique_ptr must occur in only one std::unique_ptr at any time;**
即:
任何单独的时间点,一个被unique_ptr储存的指针,只能以一个name出现在整个程序里.
This is the classic uniqueness violation: at any given point in the execution, any value held by a std::unique_ptr (or more generally, any move-only type) can only be referred to by a single distinct name. Anything that looks like a copy introducing an additional name is forbidden and won’t compile:
即:
在程序的任何执行过程里,任何只移类型都只能被一个name代表,任何看起来要复制出一个name的行为都是禁止的,且不会通过编译.
The call to std::move() is effectively a name-eraser
调用std::move实际上等于一个名字删除器,删除一个name对value的refer,然后可以选择将他给一个新的name.但是我们也实际上知道:std::move()做的也只是把一个变量cast成右值.

示例

std::unique_ptr<Foo> NewFoo() {
  return std::unique_ptr<Foo>(new Foo(1));
}

void AcceptFoo(std::unique_ptr<Foo> f) { f->PrintDebugString(); }

void Simple() {
  AcceptFoo(NewFoo());
}

void DoesNotBuild() {
  std::unique_ptr<Foo> g = NewFoo();
  AcceptFoo(g); // DOES NOT COMPILE!
}

void SmarterThanTheCompilerButNot() {
  Foo* j = new Foo(2);
  // Compiles, BUT VIOLATES THE RULE and will double-delete at runtime.
  std::unique_ptr<Foo> k(j);
  std::unique_ptr<Foo> l(j);
}

这段代码教我们数名字.

  1. Simple()的第一行只有一个name ,即 AcceptFoo(std::unique_ptr f)中的f.
  2. 加强一下DoesNotBuild()函数的作用域中有几个名字指向同一个unique_ptr呢?两个,一个是g,一个是AcceptFoo函数的参数f
  3. 当然我们也可以试着去欺骗编译器,比如SmarterThanTheCompilerButNot()函数所做的(2两个name,分别是k,l),这样可以通过编译,但是运行时也会出错,比如这里在函数执行完会把j指向的内存delete两遍.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值