9

I'm reading lippman's c++ primer where on p. 303 they give this:

class Account {
private:
  static constexpr int period = 30;
  double daily_tbl[period];
}

If the member is used only in contexts where the compiler can substitute the member's value, then an initialized const or constexpr static need not be separately defined. However, if we use the member in a context in which the value cannot be substituted, then there must be a definition for that member.

Also:

For example, if we pass Account::period to a function that takes a const int&, then period must be defined.

So I tried adding such a function:

class Account {
private:
  static constexpr int period = 30;
  double daily_tbl[period];

  void foo(const int &i) { ; }
  void bar() { foo(period); } //no error?
};

There I have added a function that takes a const int&. I also did not add any definition for the period variable. But still I get no error, as they said I should get. Why not?

user2015453
  • 4,844
  • 5
  • 25
  • 27

1 Answers1

6

A violation of this rule does not require a diagnostic. So behavior is effectively undefined.

I think that the reason this is not required to be diagnosed is because the diagnostic would be given by the linker. And when the compiler optimizes the accesses away (as it probably happened in this case), the linker cannot notice anything wrong anymore. Still noticing this error would require whole program analysis in the linker so that it has access to the original unoptimized source code representation. This increases compile time and requires an advanced linker and compiler.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • 1
    Where is the *rule* defined? – Alok Save Jan 27 '13 at 12:51
  • @alok it is the one definition rule in clause 3. – Johannes Schaub - litb Jan 27 '13 at 12:52
  • 1
    Can you please elaborate in detail in your answer? – Alok Save Jan 27 '13 at 12:54
  • 1
    Maybe 9.4.2/4 is relevant? "There shall be exactly one definition of a static data member that is odr-used (3.2) in a program; no diagnostic is required." – Andy Prowl Jan 27 '13 at 12:59
  • 1
    @alok i think that the reason is because the diagnostic would be given by the linker. and when the compiler optimizes the accesses away(as it probably happened in his case), the linker cannot notice anything wrong anymore. still noticing this error would require whole program analysis, which increases compile time and requires an advanced linker and compiler. – Johannes Schaub - litb Jan 27 '13 at 12:59
  • 1
    @andy yes that is relevant. the same thing is required by the more general 3.2p4. – Johannes Schaub - litb Jan 27 '13 at 13:04
  • 1
    @JohannesSchaub-litb: About odr-used variables, 3.2/2 says: "A variable whose name appears as a potentially-evaluated expression is odr-used **unless** it is an object that satisfies the requirements for appearing in a constant expression (5.19) **and** the lvalue-to-rvalue conversion (4.1) is immediately applied." `period` does satisfy the requirements for appearing in a constant expression, so I guess it's the second condition that does not apply (although I do not really understand it). Is that correct? – Andy Prowl Jan 27 '13 at 13:06
  • 2
    Maybe this is too silly to ask but why taking a const reference to `period` classify's as a context in which the value cannot be substituted by the compiler? Is this similar to the rationale of taking address of In-Class initialized static member which means the object has to be placed at some memory location and not merely optimized out? How so? – Alok Save Jan 27 '13 at 13:10
  • @JohannesSchaub-litb: I just can't figure out what the second condition really means. Does it mean you can basically use the `constexpr` variable only when returning its value from a function ("immediate lvalue-to-rvalue conversion"?), otherwise it is odr-used? – Andy Prowl Jan 27 '13 at 13:12
  • 1
    @andy i recommend to ask a new SO question if you seek for the meaning of this standards text. – Johannes Schaub - litb Jan 27 '13 at 13:15
  • 1
    @JohannesSchaub-litb: Then I would need to ask a question for every second sentence in a 1300 page document :-) But well, why not. Thank you for your time. – Andy Prowl Jan 27 '13 at 13:17
  • Done: http://stackoverflow.com/questions/14547986/what-am-i-allowed-to-do-with-a-static-constexpr-in-class-initialized-data-memb – Andy Prowl Jan 27 '13 at 13:47