0

As I understand it, const char* const doesn't guarantee that the pointer is the same every time you run the program, it just means the pointer and the data to which the pointer points can't be changed in the lifetime of the program.

constexpr const char* has the same effect of making the value unchangeable in every way from within the program, and (as far as I can tell) it also doesn't guarantee that the pointer is the same between runs of the program because that would contradict virtual memory starting point randomization, which wouldn't be possible if constexpr const char* made the same pointer every time.

The only difference is that constexpr const char*, when used as an arg for a constexpr function, results in a compile-time expression while const char* const, when used as an arg for a constexpr function, doesn't result in a compile-time expression. Why is that? Shouldn't the compiler be smart enough to be able to use both, since they mean the same thing, instead of enforcing this weird rule that seemingly does nothing?

Nik Tedig
  • 453
  • 2
  • 6
  • 1
    Does this answer your question? [Explain constexpr with const char\*const](https://stackoverflow.com/questions/29844028/explain-constexpr-with-const-charconst) – Sergey Kolesnik Nov 27 '21 at 18:08
  • 1
    Compilers should not be smart, they should implement the standard ;) And when you say const char* const you say you want data to be available at runtime. With constexpr you say it is fine if the string doesn't end up in the binary and is only used to evaluate stuff at compile time. So there is an important difference. – Pepijn Kramer Nov 27 '21 at 18:08
  • @SergeyKolesnik It doesn't really. The only answer is a quote from the spec, which doesn't really give me any insight into why the decision might have been made to go down this route or why the decision to do it this way makes any sense. – Nik Tedig Nov 27 '21 at 18:31
  • @PepijnKramer You make a good point, I'm still confused though. Even if constexpr makes a difference (not enforcing the presence at runtime as you said), why shouldn't const char* const work just as well as an argument to a constexpr function? What if I'm fine with it being present at runtime and I still want to do some constexpr function on it? – Nik Tedig Nov 27 '21 at 18:34
  • 1
    That is what constexpr means, if it can't be evaluated at compile time it ends up in runtime code. The C++ standard is pretty explicit about what a core constant expression is and what is not. (https://en.cppreference.com/w/cpp/language/constant_expression). I have no idea if what you're asking for has ever been proposed and if so why it has been rejected. – Pepijn Kramer Nov 27 '21 at 18:57
  • @PepijnKramer I guess I'm going to have to live with the fact that it is the way it is because the spec says so. I still don't understand why const char* const can't be evaluated at compile time, just as a constexpr const char* can. Maybe they'll add that eventually, I see no reason not to. – Nik Tedig Nov 28 '21 at 09:52

0 Answers0