Even after reading this question about non explicit inline namespace scoped variables, defined in headers, I am a bit paranoid about explicit inline namespace scope variables being ok, since AFAIK violations against the ODR are UB and not diagnosis is required. Is my understanding correct that explicitly inline specified constexpr
(and const
non volatile alike) defined variables at namespace scope are inline variables and therefore their ODR usage is ok when used in different translation units?
Even cppreference.com is contradicting itself, when it sometimes say inline variables have to be external for the ODR usage exception, while on another page only internal linkage inline variables are ok in general, and external only with additional requirements.
Basically are these assumptions right?:
/*! @file some_header.hpp */
#ifndef HEADER_GUARD
#define HEADER_GUARD
constexpr int global_non_expl_inline = 42; //UB
static constexpr int global_non_expl_inline_static = 42; //UB
inline int global_expl_inline = 42; //ok
inline int static global_expl_inline_explicit_static = 42; //? external linkage by default but static explicit but still ok?
inline int extern global_expl_inline_explicit_extern = 42; //UB
namespace foo {
constexpr int global_non_expl_inline = 42; //UB
static constexpr int global_non_expl_inline_static = 42; //UB
inline int global_expl_inline = 42; //ok
inline int static global_expl_inline_explicit_static = 42; //? external linkage by default but static explicit but still ok?
inline int extern global_expl_inline_explicit_extern = 42; //UB
}
namespace {
inline int extern global_expl_inline_explicit_extern_but_unnamed_ns = 42; //ok
}
struct bar{
static int const in_class_static = 42;//ok
static int in_class_but_out_of_source_def;
};
int bar::in_class_but_out_of_source_def = 42;//UB
#endif