In the C++11 standard, what is the difference between constexpr
and static constexpr
global variables when defined in a header? More specifically, when multiple translation units include the same header, which declaration (if any) is guaranteed to define the same variable across the translation units?
e.g.,
cexpr.h:
#ifndef CEXPR_H
#define CEXPR_H
constexpr int cint = 1;
static constexpr int scint = 1;
#endif
a.cpp:
#include "cexpr.h"
b.cpp:
#include "cexpr.h"
In your current example there is no difference: On variable declarations,
constexpr
impliesconst
, and a const variable at namespace scope has internal linkage by default (so addingstatic
does not change anything).In C++14, you cannot declare a variable as
constexpr
and have it have external linkage unless you only ever do this in one single translation unit. The reason is thatconstexpr
variables require an initializer, and a declaration with initializer is a definition, and you must only have a single definition.However, what you can do is use a normal integral constant, which you can declare (not define) as
extern
, and in the translation unit where it is defined it can even be used as a constant expression:lib.h:
lib.cpp:
In C++17, there is a new feature "inline variables" which lets you say:
And this is an "inline definition" that can appear repeatedly, and each definition defines the same entity (just like all the other "inline" entities in the language).