Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

global declarations/initializations using static, const, constexpr

Tags:

c++

c++11

In C++ or C++11, for the following declarations//initializations,

// global scope
const int a = 1; // line 1
static const int b = 2; // line 2
constexpr int c = 3;  // line 3
static constexpr int d = 4; // line 4
constexpr int e = a + b + c*d; // line 5
static constexpr int f = a - b - c*d; // line 6

This question says at file scope there is no difference between line 1 and 2 in C++. How about line 3 and 4?

Are there differences between line 4 and 5?

Are there differences between line 5 and 6?

like image 739
Allanqunzi Avatar asked Jun 30 '15 05:06

Allanqunzi


1 Answers

No, there should not be any difference (aside from their values of course) because constexpr and const implies internal linkage:

[C++11: 3.5/3]: A name having namespace scope (3.3.6) has internal linkage if it is the name of

  • a variable, function or function template that is explicitly declared static; or,
  • a variable that is explicitly declared const or constexpr and neither explicitly declared extern nor previously declared to have external linkage; or
  • a data member of an anonymous union.
like image 106
Baptiste Wicht Avatar answered Oct 05 '22 22:10

Baptiste Wicht