简体   繁体   中英

What's the difference between “” and {} for initializing an empty string?

string a = "";
string b = {};

I couldn't really find a good reference explaining the difference between them. Does a compiler see them differently? Then, why?

a is constructed using copy initialisation.

b is constructed using copy list initialisation.

For a std::string the compiler will produce the same thing; a zero length string.

But the mechanism by which the string is constructed may well be different - a compiler, conceptually at least, will have to traverse the anonymous temporary const char[] passed to construct a.

For other types there may be differences; research the two terms above for more details.

In this case, no difference.

string b = {};

initializes the string with the type's default value, which is an empty string.

string a = "";

initializes the string with a specific value, which happens to also be an empty string.

Note that just doing string c; would also create an empty string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM