Reputation: 49
1. string s6 {0};
2. string s5 {'a','b',7};
cout << "S6 ::: " << s6 << endl;
cout << "S5 ::: " << s5 << endl;
ab
not expected behaviour.Running on QT5.. compiler clang on MacOs
Upvotes: 0
Views: 66
Reputation: 310940
In the both cases
1. string s6 {0};
2. string s5 {'a','b',7};
there is used the constructor that accepts an initializer list.
In the second case the integer literal 7 can be represented in the type char. So there is no narrowing conversion.
Consider the following program.
#include <iostream>
#include <string>
int main()
{
std::string s6 {0};
std::string s5 {'a','b',7};
std::cout << s6.size() << ": ";
for ( char c : s6 ) std::cout << static_cast<int>( c ) << ' ';
std::cout << '\n';
std::cout << s5.size() << ": ";
for ( char c : s5 ) std::cout << static_cast<int>( c ) << ' ';
std::cout << '\n';
return 0;
}
Its output is
1: 0
3: 97 98 7
Upvotes: 3