Construct an empty object without the default constructor

江枫思渺然 提交于 2019-12-03 20:40:19
Language Lawyer

For your own types, you could copy- or move-construct an object from itself: F f = f. This does not lead to UB by itself, see CWG363. However, it is not so clear for a compiler-provided closure type, even if you know it is empty.

but F has no default constructor

If that's the case, then the user either explicitly deleted it or it was implicitly deleted because the user provided some other constructor. In either case, the type is not Trivial.

If an object is non-Trivial, then to create an object of that type without copying/moving from an existing instance, you must explicitly call some sort of constructor. There's no getting around that.

Even the common initial sequence rules of a union don't allow you to create the other object. It only permits access to the non-static data members of the other object. Since your object is empty, this is of no value to you.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!