Person.Junkie
Person.Junkie

Reputation: 1886

String assignment with initializer lists

Code:

#include <iostream>
#include <typeinfo>
using namespace std;

int main() {
    string s {"IDE"};
    std::cout<<typeid(s).name()<<std::endl;

    auto S{"IDE"};      // why do not deduced as string?
    std::cout<<typeid(S).name()<<std::endl;

    auto c = {"IDE"};  // why do not deduced as string?
    std::cout<<typeid(c).name()<<std::endl;   

    auto C {string{"IDE"}}; // why do not deduced as string?
    std::cout<<typeid(C).name()<<std::endl; 

    auto Z = string{"IDE"};
    std::cout<<typeid(Z).name()<<std::endl; 

}

output:

Ss
St16initializer_listIPKcE
St16initializer_listIPKcE
St16initializer_listISsE
Ss

Upvotes: 4

Views: 753

Answers (1)

R Sahu
R Sahu

Reputation: 206607

string s {"IDE"};  // Type of s is explicit - std::string

auto S{"IDE"};     // Type of S is an initializer list consisting of one char const*.

auto c = {"IDE"};  // Type of c is same as above.

auto C {string{"IDE"}}; // Type of C is an initializer list consisting of one std::string

auto Z = string{"IDE"}; // Type of Z is std::string

I don't know what PKcE stands for. I can only guess that P stands for Pointer, K stands for const, c stands for character. No clue what E could stand for.

Upvotes: 6

Related Questions