Search code examples
c++standardsexpression-evaluation

In C++ is the expression for a value to access a static member sequenced before the static member access?


Suppose I have:

struct Foo {
    static void bar();
};

Foo foo() { return Foo(); }

In the expression foo().bar() is the call to foo guaranteed to happen BEFORE the call to bar()? Where is this stated in the standard?

The doubt is because the value of the result of the call is not used, and the type is known at compile time...


Solution

  • [expr.ref] p1 states:

    The postfix expression before the dot or arrow is evaluated; the result of that evaluation, together with the id-expression, determines the result of the entire postfix expression.

    This means that for E1.E2, E1 takes place before E1.E2, but there is no sequencing of E1 and E2(1). You have a function call of the pattern foo().bar(). The function call to bar is sequenced after foo().bar as a whole.

    Therefore yes, the call to foo() happens before the call to bar().

    Interestingly, the wording does not use the term "sequenced", which is unusual and likely an editorial issue.


    (1) E2 is an id-expression (e.g. foo) which is either a qualified-id or unqualified-id, and such an expression in itself performs no value computation and has no side effects. There is nothing to be sequenced anyway.