#include <iostream>
#include <typeinfo>
int main()
{
const char a[] = "hello world";
const char * p = "hello world";
auto x = "hello world";
if (typeid(x) == typeid(a))
std::cout << "It's an array!\n";
else if (typeid(x) == typeid(p))
std::cout << "It's a pointer!\n"; // this is printed
else
std::cout << "It's Superman!\n";
}
Why is x
deduced to be a pointer when string literals are actually arrays?
A narrow string literal has type "array of n
const char
" [2.14.5 String Literals [lex.string] §8]
The feature
auto
is based on template argument deduction and template argument deduction behaves the same, specifically according to §14.8.2.1/2 (C++11 standard):If you want the type of the expression
x
to be an array type, just add&
afterauto
:Then, the
auto
placeholder will be deduced to beconst char[13]
. This is also similar to function templates taking a reference as parameter. Just to avoid any confusion: The declared type of x will be reference-to-array.Because of array-to-pointer conversion.
If
x
is to be deduced as array, only if the following is allowed:In C++, an arrray cannot be initialized with another array (like above). In such cases, the source array decays into pointer type, and you're allowed to do this instead:
The rules for type-inference with
auto
is same as the rules of type-deduction in function template:§7.1.6.4/6 says about
auto
specifier: