What is the lifetime of a default argument temporary bound to a reference parameter?

前端 未结 2 1486
难免孤独
难免孤独 2020-12-16 17:02

I thought references only extend the lifetime of temporaries to the lifetime of the reference itself, but the output of the following snippet seems contradictory:

         


        
相关标签:
2条回答
  • 2020-12-16 17:19

    Interesting, +1. (I do not mean to compete with your nice self answer here). Just a side note for anyone interested. If you want a similar effect but allowing non-const you could use move semantics:

    #include <iostream>
    
    struct X{
       ~X(){ std::cout << "Goodbye, cruel world!\n"; }
       X(X && x){  std::cout << "moved "; }
       X(){}
    };
    
    X  f(X x = X()){
      std::cout << "Inside f()\n";
      return x;
    }
    
    void g(X x){
      std::cout << "Inside g()\n";
    }
    
    int main(){
       g(f());
    }
    

    gives

    Inside f()
    moved Inside g()
    Goodbye, cruel world!
    Goodbye, cruel world!
    
    0 讨论(0)
  • 2020-12-16 17:21

    The standard handles this in a special case in §12.2 [class.temporary]:

    p4 There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. [...]

    p5 The second context is when a reference is bound to a temporary. The temporary to which the reference is bound or the temporary that is the complete object of a subobject to which the reference is bound persists for the lifetime of the reference except:

    • A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full-expression containing the call.

    The standard also has a handy note on full-expressions and the evaluation of their subexpressions with regards to default parameters in §1.9 [intro.execution] p11:

    [ Note: The evaluation of a full-expression can include the evaluation of subexpressions that are not lexically part of the full-expression. For example, subexpressions involved in evaluating default arguments (8.3.6) are considered to be created in the expression that calls the function, not the expression that defines the default argument. —end note ]

    0 讨论(0)
提交回复
热议问题