1

How to declare constexpr c-string member ? Non-member constexpr c-string can be declared this way:

constexpr char id_[] = "aaa";

so I have though that I can declare it this way:

struct T
{
    const static constexpr char id_[] = "aaa";
};

but I get undefined reference error.

Irbis
  • 1,432
  • 1
  • 13
  • 39
  • 1
    TL;DR of the dupe: Add `const constexpr char T::id_[];` outside the class to actually define it. – NathanOliver Aug 02 '19 at 19:26
  • @NathanOliver `const constexpr` is redundant. Just `constexpr` will do, since it implies `const` :) – Jesper Juhl Aug 02 '19 at 19:33
  • But const static members don't have to be defined outside. Why it doesn't work with constexpr ? – Irbis Aug 02 '19 at 19:33
  • 1
    @Irbis They still have to be defined outside, but you can initialize them inside. This has changed in C++17 with the introduction of `inline` for variables. Since `constexpr` static members are implicitly inline you just need `static constexpr char id_[] = "aaa";` inside `T`. – NathanOliver Aug 02 '19 at 19:37
  • You can use `const char T::id_[sizeof(T::id_)];` to define it outside. – jxh Aug 02 '19 at 19:45

0 Answers0