GSi
GSi

Reputation: 649

Brace-or-equal initialization through a function call

Although I think it wouldn't be a good practice, the compilers I use (both GNU and clang) seem to allow something like the following

      int f() {return 11;}

      class A {

          int a = f();
          public:
          int tell_me() {return a;}
              };

i.e. where a brace-or-equal initialization is made by calling a completely extraneous function.

Is this correct and allowed by the standard? What about the collateral effects eventually introduced by the execution of f()?

Upvotes: 1

Views: 76

Answers (1)

eerorika
eerorika

Reputation: 238301

Is this correct and allowed by the standard?

Yes.

What about the collateral effects eventually introduced by the execution of f()?

Side-effects happen when the function is executed, which happens when an A is constructed. Exactly like they would if initialization list were used.

Upvotes: 1

Related Questions