Confusion over static data initialization in class

I read lippman C ++ primer where on p. 303 they give the following:

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

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

also:

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

So I tried to add a function like this:

 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 added a function that takes const int &. I also did not add any period variable definition. But I'm still not mistaken, because they said that I should receive. Why not?

+9
c ++
Jan 27 '13 at 12:36
source share
1 answer

Violation of this rule does not require diagnostics. Thus, the behavior is effectively undefined.

I think the reason this is not required for diagnostics is because the diagnostics will be provided by the linker. And when the compiler optimizes access (as it probably is in this case as well), the linker no longer notices anything bad. Still noticing this error, all the analysis of the program in the linker is required so that it has access to the original unoptimized representation of the source code. This increases compilation time and requires an advanced linker and compiler.

+6
Jan 27 '13 at 12:49 on
source share
— -



All Articles