Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]?

like image 734
Sir Visto Avatar asked Jan 10 '19 20:01

Sir Visto


1 Answers

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.

like image 163
NathanOliver Avatar answered Oct 05 '22 11:10

NathanOliver