5

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?

Community
  • 1
  • 1
Allanqunzi
  • 3,230
  • 1
  • 26
  • 58
  • 2
    "What does all this code mean" is an awfully broad question. A book is probably your best destination if you don't know what `const` or `static` or `constexpr` means. – Drew Dormann Jun 30 '15 at 05:06
  • 1
    That's a useful edit, although the code here is not the same as the code in the question you link to. I'm *guessing* that you're either pursuing an open-ended discussion about variable declarations, or you're asking what `static` means. – Drew Dormann Jun 30 '15 at 05:13
  • According to my understanding, `static` means the variable exists until the end of the program, but when put `static` together with `constant` or `constexpr` in global scope, `static` doesn't add additional meaning to the variable when the variable is initialized only with `const` or `constexpr`. – Allanqunzi Jun 30 '15 at 05:20
  • Without picking word meanings apart, I think you understand what `static` means in the context of a function body, but you don't know [what it means when applied to variables that would otherwise be global](http://stackoverflow.com/questions/2271902/static-vs-global). – Drew Dormann Jun 30 '15 at 05:30
  • The link you gave talks about linkage and file scope. But `const` or `constexpr` has default internal linkage, so `static` doesn't add any meaning to the variable which have been declared by `const` or `constexpr` like line 3 and 5, so my understanding is that line 3 and 4 have no differences, neither do line 5 and 6. – Allanqunzi Jun 30 '15 at 05:46

2 Answers2

7

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.
Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110
0

static variables exist for the lifetime of the program static is useful for functions like this:

void func (int i) {
    int var = i;
}

when a function is finish executing its code its objects destroys automatically to prevent this you can use static

void func (int i) {
   static int var = i;
}

this mean that when a function finish executing its code the object defined as static will remain until the program ends

const applies for variables, and prevents them from being modified in your code. and constexpr are used for constant expressions this two is read-only it means that once you initialize a value it cannot be modified

the difference of this two is:

static constexpr int d = 4; // line 4
constexpr int e = a + b + c*d; 

in static constexpr int d = 4 is we define a variable named d that is a static constant expression integer and have a value of 4 and cannot be modified and remain until the program ends

and in constexpr int e = a + b + c*d; is we define a variable name e that is constant expression integer that have a value depends on what the result in those operations and cannot be modified

Kryssel Tillada
  • 180
  • 3
  • 13