3

Why don't we make all string literals constexpr?

constexpr char name[] = "hello";

template < const char* N >
struct T{};

T < name > t;// this works fine
T < "hello" > t1; // this doesn't work
Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
  • [See here](https://stackoverflow.com/a/46100477/1505939) for an alternative – M.M May 15 '19 at 06:35
  • 1
    Your template takes a char* not a char[], could this mismatch in types make a difference? – JVApen May 15 '19 at 06:47
  • https://stackoverflow.com/questions/28809728/some-const-char-are-unavailable-at-compile-time – VLL May 15 '19 at 07:11
  • https://stackoverflow.com/questions/31504457/c-non-type-template-parameter-const-char – VLL May 15 '19 at 07:12
  • 2
    Basically, because two uses of "the same" string literal `"hello"` and `"hello"` are not required to be the same object. They can be two separate strings, just as the two "hello" strings in my first sentence are in two different places. So how can the compiler determine (especially when the definitions occur in two different translation units) whether `T<"hello">` and `T<"hello">` are the same type? – Pete Becker May 15 '19 at 12:55

0 Answers0