C ++ - Unified initializer with std :: string

I am trying to use a unified intializer with a C ++ string class. Below is the code:

#include <iostream> #include <string> using namespace std; int main() { string str1 {"aaaaa"}; string str2 {5, 'a'}; string str3 (5, 'a'); cout << "str1: " << str1 << endl; cout << "str2: " << str2 << endl; cout << "str3: " << str3 << endl; return 0; } 

The conclusion will be:

 str1: aaaaa str2: a str3: aaaaa 

It made me scratch my head. Why can't str2 achieve the desired result like str3 ?

+5
source share
2 answers

std::string has a constructor that takes an initializer_list argument.

 basic_string( std::initializer_list<CharT> init, const Allocator& alloc = Allocator() ); 

This constructor always takes precedence when you use a list with an initialization binding to build std::string . Other constructors are only considered if elements in parentheses-init-list are not converted to the element type in initializer_list . This is mentioned in [over.match.list] / 1 .

Initially, candidate functions are initializer constructors ([dcl.init.list]) of class T , and the argument list consists of a list of initializers as one argument.

In your example, the first argument 5 implicitly converted to char , so the initializer_list constructor is viable and is selectable.

This is obvious if you print each character in lines as int s

 void print(char const *prefix, string& s) { cout << prefix << s << ", size " << s.size() << ": "; for(int c : s) cout << c << ' '; cout << '\n'; } string str1 {"aaaaa"}; string str2 {5, 'a'}; string str3 (5, 'a'); print("str1: ", str1); print("str2: ", str2); print("str3: ", str3); 

Output:

 str1: aaaaa, size 5: 97 97 97 97 97 str2: a, size 2: 5 97 str3: aaaaa, size 5: 97 97 97 97 97 

demo version

+2
source

You are using the std::string constructor using this syntax

 std::basic_string( std::initializer_list<CharT> init, const Allocator& alloc = Allocator() ); 

And the compiler treated 5 as a char type, which translates to an ASCII type that does not print on the screen. If you change the value of 5 to the value to print, say A , whose ASCII value is 65 ,

 string str2 {65, 'a'}; 

he will print:

 Aa 

See Live on Coliru for more illustration.

+8
source

All Articles