temporary object, function parameters and implicit cast

前端 未结 3 1761
遇见更好的自我
遇见更好的自我 2021-02-11 05:20

In the following scenario:

struct Foo
{
   // ...
   operator Bar() {... }  // implicit cast to Bar
}

Foo GetFoo() { ... }
void CallMeBar(Bar x) { ... }

// ...         


        
3条回答
  •  执笔经年
    2021-02-11 05:39

    Regardless of the cast, the temporary object(s) will "survive" the call to CallMe() function because of the C++ standard:

    12.2.3 [...] Temporary objects are destroyed as the last step in evaluating the fullexpression (1.9) that (lexically) contains the point where they were created. [...]

    1.9.12 A fullexpression is an expression that is not a subexpression of another expression.

提交回复
热议问题