Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

static, constexpr, const - what do they mean when all used together?

I'm totally frustrated with these specifiers because I understand what they do when they're by themselves but I find them hard to understand when they're used with each other. For example, some code in the wild contained -

namespace{
static constexpr char const *Hello[] = { "HelloString", "WorldString"};
...
}

what does this even do?

  • why use static when you're already inside an anonymous namespace. And static inside classes makes sense ( unless you're writing C which lacks namespaces), without classes - why??
  • why use constexpr - there's no reason to use it here. wouldn't a simple const would do?
  • and then const *Hello doesn't makes sense to me. What is constant here? The strings or the pointer *Hello?

And worst of all, it compiles :/. Ofcourse it would compile because they're valid statements, but what does it even means?

like image 966
hg_git Avatar asked Feb 24 '16 06:02

hg_git


People also ask

What does constexpr const mean?

The keyword constexpr was introduced in C++11 and improved in C++14. It means constant expression. Like const , it can be applied to variables: A compiler error is raised when any code attempts to modify the value.

What does static constexpr mean?

static defines the object's lifetime during execution; constexpr specifies that the object should be available during compilation. Compilation and execution are disjoint and discontiguous, both in time and space. So once the program is compiled, constexpr is no longer relevant.

What is the difference between const and constexpr?

The principal difference between const and constexpr is the time when their initialization values are known (evaluated). While the values of const variables can be evaluated at both compile time and runtime, constexpr are always evaluated at compile time.

Why does constexpr need to be static?

A static constexpr variable has to be set at compilation, because its lifetime is the the whole program. Without the static keyword, the compiler isn't bound to set the value at compilation, and could decide to set it later.


1 Answers

Why use static when you're already inside an anonymous namespace?

I don't see a reason to here, unless it is written for C++03 compatibility.

Why use constexpr - there's no reason to use it here. wouldn't a simple const would do?

Again, it isn't necessary here, but it does indicate that it is a compile time constant. Also note that constexpr here means the pointer is constexpr, and removes the need for const after the * (see next part).

const *Hello doesn't makes sense to me. What is constant here? The strings or the pointer *Hello?

const applies to the left, unless there is nothing there in which case it applies to the right. const to the left of * means the pointer when dereferenced is a constant, while to the right means it is a constant pointer to something.

char const * ptr = "Foo";
ptr[0] = 'B'; //error, ptr[0] is const

char * const ptr = "Foo";
ptr = new char[10]; //error, ptr is const

char const * const ptr = "Foo"; //cannot assign to either ptr or *ptr
constexpr char const* ptr = "Foo"; //same as above, constexpr replaced last const

I found that this page on the "right left" rule really helps with understanding complicated declarations.

like image 93
Weak to Enuma Elish Avatar answered Nov 28 '22 10:11

Weak to Enuma Elish