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)
create1Run Code Online (Sandbox Code Playgroud)std::unique_ptr<Foo, destroy1>(reinterpret_cast<Foo*>(p.release()), destroy1());
这不起作用,因为你使用了错误的指针.
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现在.使用它们.