using constants in header file with ODR compliance

1.5k views Asked by At

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

There are 1 answers

1
M.M On BEST ANSWER

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.