Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading