Search code examples
c++stringvectortype-deduction

What type do vector strings deduce to?


I'm becoming familiar with using vectors (reading An introduction to std::vector), and it displays the following code as an example:

// as with std::array, the type can be omitted since C++17
std::vector array4 { 9, 7, 5, 3, 1 }; // deduced to std::vector<int>

If I use this C++17 style vector initialization with string literals, what type will the vector deduce (i.e. std::string, char*, etc)?

std::vector strArray {"Hello", "world", "!!!"};

Solution

  • (i.e. std::string

    No, string literals are not related to the class std::string. Technically this could happen if vector had a special deduction guide for this, but it doesn't.

    ... char*

    No, string literals are const.

    The contained type will be deduced as const char*.