7

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?

6
  • constexpr suggests that it is using C++11. Knowing this static makes sense again because it is going to make the initialization thread safe. (magic statics). And constexpr makes it usable in places where it wouldn't work with const. Commented Feb 24, 2016 at 7:00
  • I don't think initializing a scalar with a braced init-list compiles. Commented Feb 24, 2016 at 7:01
  • 2
    Why would magic statics and thread safety matter for load time initialization though...? What kind of parallel execution would be happening there? Commented Feb 24, 2016 at 7:07
  • If you don't mind, I've edited the question to something that looks less "downvoteable on inspection". Please roll back if you don't like it. Commented Feb 24, 2016 at 7:52
  • Are you sure that it compiles? I see a list of literals. Assuming that there is only one string, not a list, the line looks like a line copied from a class, in which case it would make sense. Commented Feb 24, 2016 at 8:23

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.