Confusion about in-class initialization of static data members

后端 未结 1 1274
醉酒成梦
醉酒成梦 2020-12-06 11:48

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];
}         


        
相关标签:
1条回答
  • 2020-12-06 12:44

    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.

    0 讨论(0)
提交回复
热议问题