I’ve never seen this call to char() as a function before. Where is this described and what does it mean? This usage is part of the example on this cppreference.com community wiki page: https://en.cppreference.com/w/cpp/string/basic_string/resize:
short_string.resize( desired_length + 3 );
std::cout << "6. After: \"";
for (char c : short_string) {
std::cout << (c == char() ? '@' : c); // <=== HERE ===
}
This wording in the description also doesn’t make any sense to me and I don’t understand what it’s saying:
Initializes appended characters to
CharT().
Highlighted in context:
>Solution :
It’s the constructor† for char; with no arguments it constructs '\0'. Rarely used since primitives offer other ways to initialize them, but you initialize them with () just like you would a user-defined class, which ensures they get initialized to something; char foo; has undefined value, while char foo = char(); or char foo{}; is definitely '\0'.
†As HolyBlackCat notes, it’s not technically a constructor, because it’s not a class, but it behaves like one for most purposes.
