Is copy/move elision allowed to make a program using deleted functions well-formed?

后端 未结 1 1750
广开言路
广开言路 2021-01-01 00:58

Consider the following code:

#include 

struct Thing
{
    Thing(void)                       {std::cout << __PRETTY_FUNCTION__ <<         


        
相关标签:
1条回答
  • 2021-01-01 01:46

    It doesn't make an ill-formed program build. It gets rid of the reference to the deleted function entirely. The appropriate wording in the proposal is here:

    [dcl.init] bullet 17.6

    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. [ Example: T x = T(T(T())); calls the T default constructor to initialize x. ]

    The example further strengthens this. Since it indicates the whole expression must collapse into a single default construction.

    The thing to note is that the deleted function is never odr-used when the copies are elided due to value categories, so the program is not referring to it.

    This is an important distinction, since the other form of copy elision still odr-uses the copy c'tor, as described here:

    [basic.def.odr]/3

    ... A constructor selected to copy or move an object of class type is odr-used even if the call is actually elided by the implementation ([class.copy] ...

    [class.copy] describes the other form of permissible (but not mandatory) copy-elision. Which, if we demonstrate with your class:

    Thing foo() {
        Thing t;
        return t; // Can be elided according to [class.copy.elision] still odr-used
    }
    

    Should make the program ill-formed. And GCC complains about it as expected.


    And by the way. If you think the previous example in the online compiler is a magicians trick, and GCC complains because it needs to call the move c'tor. Have a look at what happens when we supply a definition.

    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题