Consecutively calling temporary object methods

Conforms to the standard for the following code:

struct Temp
{
    Temp& op1() { ...; return *this; }
    Temp& op2() { ...; return *this; }
    // more op...
};

Temp().op1().op2()....;   // safe or not? Which paragraph from ISO 12.2 qualifies it?
+4
source share
2 answers

Totally safe.

This is in paragraph 3 (section "12.2, [class.temporary]):

Temporary objects are destroyed as the last step in evaluating the full expression (1.9), which (lexically) contains the point at which they were created.

& sect; 1.9 / 10 ( [intro.execution]) defines the full expression:

A full expression is an expression that is not a subexpression of another expression.

and includes an example somewhat similar to your question:

void f() {
  if (S(3).v()) // full-expression includes lvalue-to-rvalue and
                // int to bool conversions, performed before
                // temporary is deleted at end of full-expression
  { }
}

N3691, 2013 ++ - , , , ++ 1x , , C + + 1y (x & cong; 4; y & cong; 7)

+7

:

struct Temp
{
    Temp& op1() { ...; return *this; }
    Temp& op2() { ...; return *this; }
    // more op...
}

- .

.


, , -, const -, .

, ( ).

+3

Source: https://habr.com/ru/post/1539863/


All Articles