澄清P0137的细节

Ric*_*ges 11 c++ language-lawyer c++17

在下面的代码中,我在标准的单词(加上P0137的措辞)中对对象生命周期进行了细致的处理.

请注意,根据P0137,所有内存分配都是通过适当对齐的unsigned char类型存储.

还要注意,这Foo是一个POD,带有一个简单的构造函数.

问题:

A.如果我误解了标准,并且这里有任何UB,请将其指出(或者确认没有UB)

B. 鉴于构造是微不足道的,并且不执行实际的初始化,A,B,C,D,E,F的初始化是严格必要的.如果是这样,请说明标准的哪一部分在这方面与[object.lifetime]相矛盾或澄清.

码:

#include <memory>

// a POD with trivial constructor
struct Foo 
{
  int x;
};

struct destroy1
{
  void operator()(Foo* p)
  {
    // RAII to guarantee correct destruction order
    auto memory = std::unique_ptr<unsigned char[]>(reinterpret_cast<unsigned char*>(p));
    p->~Foo(); // A
  }
};
std::unique_ptr<Foo, destroy1> create1()
{
  // RAII to guarantee correct exception handling
  auto p = std::make_unique<unsigned char[]>(sizeof(Foo));
  auto pCandidate = reinterpret_cast<Foo*>(p.get());
  new (pCandidate) Foo(); // B
  return std::unique_ptr<Foo, destroy1>(reinterpret_cast<Foo*>(p.release()), 
                                        destroy1());
}

struct call_free
{
  void operator()(void *p) const { std::free(p); } 
};
using malloc_ptr = std::unique_ptr<unsigned char, call_free>;

struct destroy2
{
  void operator()(Foo *pfoo) const {
    // RAII to guarantee correct destruction order
    auto memory = malloc_ptr(reinterpret_cast<unsigned char*>(pfoo));
    pfoo->~Foo(); // C
  }
};

std::unique_ptr<Foo, destroy2> create2()
{
    // RAII to guarantee correct exception handling
  auto p = malloc_ptr(reinterpret_cast<unsigned char*>(std::malloc(sizeof(Foo))));
  auto pCandidate = reinterpret_cast<Foo*>(p.get());
  new (pCandidate) Foo(); // D
  return std::unique_ptr<Foo, destroy2>(reinterpret_cast<Foo*>(p.release()), 
                                        destroy2());
}

struct nodelete {
  void operator()(Foo * p) {
    p->~Foo();  // E
  }
};

std::shared_ptr<Foo> provide()
{
  alignas(Foo) static unsigned char  storage[sizeof(Foo)];

  auto make = [] {
    auto p = reinterpret_cast<Foo*>(storage);
    new (p) Foo (); // F
    return std::shared_ptr<Foo>(p, nodelete());
  };

  static std::shared_ptr<Foo> pCandidate = make();

  return pCandidate;
}


int main()
{
  auto foo1 = create1();
  auto foo2 = create2();
  auto foo3 = provide();

  foo1->x = 1;
  foo2->x = 2;
  foo3->x = 3;
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*las 6

上 create1

std::unique_ptr<Foo, destroy1>(reinterpret_cast<Foo*>(p.release()), destroy1());
Run Code Online (Sandbox Code Playgroud)

这不起作用,因为你使用了错误的指针.

p.release()认为它指向一个unsigned char[].但是,这不是您想要指向的对象.你想要指出的是Foo你创建的数组中的对象.

所以你现在受[basic.life]/8的约束.其要点是,如果它们属于同一类型,则只能使用前一个指针作为指向新对象的指针.他们不是你的情况.

现在,我可以告诉你launder指针,但更合理的方法是只存储placement-new调用返回的指针:

auto p = std::make_unique<unsigned char[]>(sizeof(Foo));
auto ret = std::unique_ptr<Foo, destroy1>(new(p.get()) Foo(), destroy1());
p.release();
return ret;
Run Code Online (Sandbox Code Playgroud)

该指针始终是正确的.

您对placement-new的使用不是可选的.[intro.object]/1告诉我们:

当隐式更改并集的活动成员(9.3)或创建临时对象时(4.4,12.2),对象由定义(3.1),new-expression(5.3.4)创建.

分配时unsigned char[],这是您在该存储中创建的对象.你不能简单地假装它是一个Foo,只是因为它Foo是一个聚合.[intro.object]/1不允许这样做.您必须通过上面列出的某种机制显式创建该对象.由于您不能使用定义,union成员激活或具有任意内存缓冲区的临时对象来从现有存储创建对象,因此您必须创建对象的唯一方法是使用new-expression.

具体来说,安置新.

至于delete1,你需要一个定制删除,因为默认删除器将调用delete的Foo指针.您的代码如下:

auto memory = std::unique_ptr<unsigned char[]>(reinterpret_cast<unsigned char*>(p));
p->~Foo();
Run Code Online (Sandbox Code Playgroud)

unsigned char[]由于[intro.object]/3-4,对于在存储中分配对象时的行为方式,它有一些特殊的逻辑.如果对象完全覆盖了存储unsigned char[],那么它的功能就像对象是在数组中分配的一样.这意味着它unsigned char[]仍然在技术上存在; 它没有破坏字节数组.

因此,您仍然可以删除代码在此处执行的字节数组.

上 create2

由于进一步违反[basic.life]/8,这也是错误的.固定版本与上述类似:

auto p = malloc_ptr(reinterpret_cast<unsigned char*>(std::malloc(sizeof(Foo))));
auto ret std::unique_ptr<Foo, destroy2>(new(p.get()) Foo(), destroy2());
p.release();
return ret;
Run Code Online (Sandbox Code Playgroud)

与new-expressions不同,malloc永远不会通过[intro.object]/1创建对象; 它只获得存储空间.因此,再次需要placement-new.

同样,free只是释放内存; 它不处理对象.所以你delete2的基本上很好(虽然使用malloc_ptr它会让它不必要地混淆).

上 provide

这与你的其他示例有相同的[basic.life]/8问题:

alignas(Foo) static unsigned char storage[sizeof(Foo)];
static auto pCandidate = std::shared_ptr<Foo>(new(storage) Foo(), nodelete());
return pCandidate;
Run Code Online (Sandbox Code Playgroud)

但除此之外,它没关系(只要你不在其他地方打破它).为什么?那太复杂了.

[basic.start.term]/1告诉我们静态对象以其初始化的相反顺序被销毁.[stmt.decl]/4告诉我们,块作用域静态对象按函数中遇到的顺序进行初始化.

因此,我们知道之前pCandidate会被摧毁.只要你没有在静态变量中保留它的副本,或者在终止之前不能销毁/重置所有这样的共享对象,你应该没问题. storageshared_ptr


总而言之,使用块unsigned char实际上是预先C++ 11.我们现在std::aligned_storage和std::aligned_union现在.使用它们.

  • `unsigned char []`很特别.它*为你放置的对象 - "新"提供存储空间([\ [intro.object \]/3](https://timsong-cpp.github.io/cppwp/intro.object#3)) ,以及该对象*嵌套在*表示`unsigned char []`([\ [intro.object \]/4](https://timsong-cpp.github.io/cppwp/intro.object#4) ),这意味着placement -`new`不会结束`unsigned char []`的生命周期([\ [basic.life \] /1.4](https://timsong-cpp.github.io/ cppwp/basic.life#1.4))因为它的存储被嵌套在其中的对象重用. (3认同)
  • ……忘了说,顺便说一句,这是一个很好的答案。信息量很大。谢谢你。 (2认同)
  • 这是一个令人震惊的启示!它本质上是说调用任何 c 库(除了 c 运行时库的可能部分)是实现定义的(即不可靠)。这肯定不是标准委员会的意图吗?在我看来,这太重要了,不能简单地将其视为“实现定义”。符合 c 标准的库要么使用 c++,要么不使用——否则整个计算大厦都是建立在愿望和彩虹之上的,不是吗? (2认同)
  • 由于没有终身结束,[basic.life]/8不适用.这里的问题是`reinterpret_cast`强制转换的结果并没有指向`Foo`对象,因为有争议的对象不是指针可互换的(参见[\ [expr.static.cast \]/13](https ://timsong-cpp.github.io/cppwp/expr.static.cast#13)).相反,"转换时指针值不变",这意味着它仍然指向`unsigned char []`数组的第一个元素,即使它的类型是`Foo*`. (2认同)