简体   繁体   中英

Relaxation of copy initialization requirements in C++17

I'm confused by the cppref statements:

[...] The result of the conversion, which is a prvalue expression (since C++17) if a converting constructor was used, is then used to direct-initialize the object. The last step is usually optimized out and the result of the conversion is constructed directly in the memory allocated for the target object, but the appropriate constructor (move or copy) is required to be accessible even though it's not used. (until C++17)

I tested the code std::atomic_int atom = 1; on gcc 8.0.1, and it compiles with C++17 but fails with C++14 with the following error:

error: use of deleted function 'std::atomic<int>::atomic(const std::atomic<int>&)'
   std::atomic_int atom = 1;
                          ^

Does that mean the appropriate constructor (move or copy) is (always?) no longer required to be accessible in C++17?

In cases where copy elision is mandatory in C++17, these constructors are never used. In that case there is nothing to check.

In earlier versions, where elision was merely optional, the access checks were required to get consistent result between compilers. Still happens in C++17 for the cases where the elision is not mandatory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM