google abseil c++ tips[55] name counting and unique_ptr 变量名计数和unique_ptr
迪丽瓦拉
2024-03-07 06:40:46
0

前置知识

  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 NewFoo() {return std::unique_ptr(new Foo(1));
}void AcceptFoo(std::unique_ptr f) { f->PrintDebugString(); }void Simple() {AcceptFoo(NewFoo());
}void DoesNotBuild() {std::unique_ptr 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 k(j);std::unique_ptr 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两遍.

相关内容