Search code examples
c++templatesstring-literalsdecltype

Template type deduction for char array


template<typename T>
class X;

int main() {
    X<decltype("")> x;
}

Why does g++ deduce T as const char (&)[1] and not simply const char[1]?


Solution

  • Unlike every other literal that is an rvalue, string literals are lvalues. decltype applied to an lvalue expression gives you a reference so const char (&)[1] is the correct behavior.