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

I am completely upset by these qualifiers because I understand what they do when they are on their own, but I find it hard to understand when they are used with each other. For example, some code in the wild contained -

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

what does it do?

  • why use static when you are already in an anonymous namespace. And static inner classes make sense (unless you write C that lacks a namespace), without classes - why ??
  • why use constexpr - there is no reason to use it here. won't there be a simple const ?
  • and then const *Hello doesn't make sense to me. What is always here? Strings or pointer *Hello ?

And worst of all, it compiles: /. Of course, it will compile because they are valid statements, but what does it mean?

+8
c ++ static c ++ 11 const constexpr
source share
1 answer

Why use static when you are already in an anonymous namespace?

I see no reason for this if it is not written for compatibility with C ++ 03.

Why use constexpr - there is no reason to use it here. won't a simple const work?

Again, this is not necessary, but this indicates that it is a compile-time constant. Also note that constexpr here means that the pointer is constexpr , and eliminates the need for const after * (see the next part).

const *Hello doesn't make sense to me. What is always here? String or pointer * Hello?

const applies to left if there is nothing there, in which case it applies to right. const left of * means that the pointer is a constant when dereferenced, and to the right means that 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 in the "right" rule really helps with understanding complex ads.

+6
source share

All Articles