using constants in header file with ODR compliance

拜拜、爱过 提交于 2019-12-09 01:02:25

问题


Looking at another question I realized that I can't use objects or functions from an anonymous namespace through a header file since it'll cause ODR violations in class definitions or inline functions. If this is the case, then is it possible to use named const or constexpr static objects in inline functions or in classes safely? For example, if CONSTANT was inside of namespace below it would be unsafe, but is it okay to use a constant with static linkage?

// some header file to be included by multiple .cpp files
static const/*expr*/ int CONSTANT = 2;

inline int f() {
  return CONSTANT;
}

class Cls {
  int mem = CONSTANT;
};

回答1:


This code is OK. The full paragraph (C++14 [basic.def.odr/6.2]) is:

in each definition of D, corresponding names, looked up according to 3.4, shall refer to an entity defined within the definition of D, or shall refer to the same entity, after overload resolution and after matching of partial template specialization, except that a name can refer to a non-volatile const object with internal or no linkage if the object has the same literal type in all definitions of D, and the object is initialized with a constant expression, and the object is not odr-used, and the object has the same value in all definitions of D; and

This usage does match all of the conditions in the "except ... and ... and ..." part:

  • The name CONSTANT does in fact refer to a non-volatile const object with internal linkage
  • It has the same literal type in all definitions of f().
  • It is initialized with a constant expression 2.
  • It is not odr-used.
  • It has the same value in all definitions of f().

The point "It is not odr-used" is supposed to mean "It is not odr-used within f()" -- i.e. it doesn't break f() if you happen to odr-use CONSTANT elsewhere in the program.



来源:https://stackoverflow.com/questions/41133290/using-constants-in-header-file-with-odr-compliance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!