本文介绍了新标准中的memcpy生命周期初始化规则有什么变化(如果有)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,memcpy不能安全地用于创建未复制存储的 源对象.

As far as I am aware, memcpy into uninitialized storage cannot safely be used to create an copy of the source object.

但是,在此线程中,最后在开放标准WG21"ub"列表的第一年,参与者引用了新的memcpy生命周期起始规则:

However, in this thread from last year on the open-std WG21 "ub" list, a participant refers to the new memcpy lifetime-initiation rules:

基于问题的上下文和少量的类型擦除示例代码,看来这可能与通过memcpyaligned_storage中创建新对象有关.

Based on the context of the question and small amount of type-erasure example code, it appears that it may be related to creating new objects in aligned_storage via memcpy.

尽我所能搜索,找不到对新规则的引用.我特别好奇它们是否仅适用于替换已创建对象的内容,或者它们是否改变了在未初始化内存中潜在创建对象的情况.

Search as I might, I can't find a reference to the new rules. I'm particularly curious if they only apply to replacing the contents of an already created object, or if they change the situation around the potential creation of an object in uninitialized memory.

推荐答案

措词有所变化,但总体思路保持不变.来自 intro.object :

The wording has changed a bit, but the general idea remains the same. From intro.object:

这是 only 的四种使用C ++创建对象的方法. memcpy不会属于这四个条件中的任何一个,因此它现在(且从未有过)不会创建生存期(可以隐式地更改联合的活动成员可以完成仅通过= ,而不是通过memcpy).

Those are the only four ways to create an object in C++. memcpy does not fall into any of those four conditions and hence it does not now (and never has before) create lifetime (implicitly changing the active member of a union can be done via = only, not via memcpy).

引用语是对标准的假设性未来更改,该更改会在某些情况下使memcpy具有这种能力.有一个很长的线程在今年早些时候也是如此.

The quote refers to a hypothetical future change to the standard that would bless memcpy with such ability in certain situations. There was a very long thread on the subject earlier in the year as well.

这篇关于新标准中的memcpy生命周期初始化规则有什么变化(如果有)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:32