c++ - 默认 move 构造函数中的赋值顺序是什么?

标签 c++ memory move-semantics allocator move-constructor

我目前正在使用 C++ 开发自定义分配器。该分配器必须通过 move 内存和对象来定期对其内存进行碎片整理。这些移位总是向下的,这意味着 move 的内存块的地址在 move 时总是减少。当旧内存块和新内存块不重叠时这样做没有问题。如果它们重叠,我首先必须将对象 move 到分配器内存之外的临时区域,然后将其移回新的内存块。

如果 std::is_trivially_move_constructible 对于被 move 的类型为真,那么如果默认 move 构造函数中的赋值顺序定义明确,我可能会将这个额外的 move 保存到临时内存块中。这引出了我的问题:分配的顺序是明确定义的还是特定于平台的?

最佳答案

来自标准(第 15.8.1 节 [class.copy.ctor])

(14) The implicitly-defined copy/move constructor for a non-union class X performs a memberwise copy/move of its bases and members. [ Note: Default member initializers of non-static data members are ignored. See also the example in 15.6.2. — end note ] The order of initialization is the same as the order of initialization of bases and members in a user-defined constructor (see 15.6.2)

点击链接将我们带到 15.6.2节[class.base.init]

(13.3) Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializer s)


没有完全回答问题,但正如 Igor Tandetnik 在他的回答中所说,将普通构造函数转换为 std::memmove

是合法的

关于c++ - 默认 move 构造函数中的赋值顺序是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44863382/

相关文章:

c++ - 禁用 Qt 组合框中的项目

c++ - 为什么这行代码会导致计算机死机?

c++ - 如何从一行中提取单个字符并将其放入 const char*

c++ - 在 bison/flex 中使用左递归时如何声明持久变量?

c - 在c中使用free()出现段错误

c++ - 自定义类中的 move 与复制性能

c++ - 为什么在返回兼容类型时需要显式 std::move?

c++ - move 由逗号运算符抑制的构造函数

c++ - 映射/设置迭代器不可递增映射/设置迭代器不可递增

c# - 在 C# 中测量内存压力/GC 执行