Difference between constexpr variable and static constexpr

In the C ++ 11 standard, what is the difference between global variables constexprand static constexprwhen defined in the header? More specifically, when several translation units include the same heading, what announcement (if any) is guaranteed to define the same variable in translation units?

eg.

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"
+4
source share
1 answer

In your current example, there is no difference: when declaring variables, constexprit implies const, and the variable const in the namespace has an internal default binding (so adding staticdoes not change anything).

++ 14 constexpr , . , constexpr , - , .

, ( ) extern, , , :

lib.h:

extern const int a;

lib.cpp:

#include "lib.h"

const int a = 10;

int b[a] = {1, 2, 3};   // OK in this translation unit

++ 17 " ", :

inline constexpr int a = 10;

" ", , ( "" ).

+4

Source: https://habr.com/ru/post/1684841/


All Articles