initialize std::array without copying/moving elements

守給你的承諾、 提交于 2019-12-05 05:28:30

You can use brace enclosed initializers instead of temporary c objects:

std::array<c,2> array = {{{"",""},{"",""}}};

or

std::array<c,2> array{{{"",""},{"",""}}};

It would become possible since C++17, from that for some specified cases copy elision is guaranteed.

Under the following circumstances, the compilers are required to omit the copy- and move- constructors of class objects even if copy/move constructor and the destructor have observable side-effects:

  • In initialization, if the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object:

    T x = T(T(T())); // only one call to default constructor of T, to initialize x
    

And for these cases, copy/move constructor is not required to be accessible.

When copy-elision takes place (until C++17) In those cases where copy-elision is not guaranteed, if it takes place (since C++17) and the copy-/move-constructor is not called, it must be present and accessible (as if no optimization happened at all), otherwise the program is ill-formed.

LIVE

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