1

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:

enter image description here

Adjacently related

  1. What motivated me to study the std::string::resize() method was trying to learn how to pre-allocate a std::string for use in C function calls as a char* buffer. This is possible by first pre-allocating the std::string by calling the my_string.resize() function on it. Then, you can safely write into &my_string[0] as a standard char* up to index my_string.size() - 1. See also:
    1. Directly write into char* buffer of std::string
    2. Is there a way to get std:string's buffer
    3. How to convert a std::string to const char* or char*
      1. See my detailed answer to this question here.
Gabriel Staples
  • 22,024
  • 5
  • 133
  • 166

2 Answers2

5

It returns 0 with the specified type, same as char(0). It's called value initialization.

The syntax mimics that of calling a default constructor for a class.

HolyBlackCat
  • 63,700
  • 7
  • 105
  • 170
  • Thanks! For anyone else wondering, I found the cppreference community wiki documentation for _value initialization_ here: https://en.cppreference.com/w/cpp/language/value_initialization – Gabriel Staples May 24 '22 at 19:02
3

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.

πάντα ῥεῖ
  • 85,314
  • 13
  • 111
  • 183
ShadowRanger
  • 124,179
  • 11
  • 158
  • 228
  • This is super weird to me. Can class-like "constructors" be called on _any_ type? Ex: `int()` is a `0` of type `int`? `uint64_t()` is a `0` of type `uint64_t`? – Gabriel Staples May 24 '22 at 17:37
  • 3
    It's not a class, so it doesn't have constructors. Pardon my nitpicking. – HolyBlackCat May 24 '22 at 17:37
  • 1
    `char foo();` is most-vexing-parse. – Richard Critten May 24 '22 at 17:37
  • @GabrielStaples: Yep, `int()` is `0`, `uint64_t()` is `UINT64_C(0)`, etc. – ShadowRanger May 24 '22 at 17:38
  • @RichardCritten Apparantely `char foo();` is not considered most-vexing parse. See [Is most vexing parse a formally defined concept](https://stackoverflow.com/questions/71937565/is-most-vexing-parse-a-formally-defined-concept) – Anoop Rana May 24 '22 at 17:39
  • @AnoopRana clang/gcc beg to differ - live - https://godbolt.org/z/PdhEMKqr3 – Richard Critten May 24 '22 at 17:41
  • 2
    @RichardCritten It's a vexing parse alright, but not the _**most** vexing parse_. :-) – Ted Lyngmo May 24 '22 at 17:44
  • 2
    @GabrielStaples `T()` creates an object of type `T` for any object type `T` and _value-initializes_ it (i.e. calls its default constructor or zero-initializes it). A constructor _may_ be involved for class types, but won't be for non-class types or POD-like class types. – user17732522 May 24 '22 at 17:47
  • @RichardCritten I too think that it is most-vexing parse, i even commented there that it should be called MVP. But by seeing the answers posted [there](https://stackoverflow.com/questions/71937565/is-most-vexing-parse-a-formally-defined-concept), and them pointing out that the author Scott himself **does not** consider this MVP, i don't think there is any technically correct definition that is right here. – Anoop Rana May 24 '22 at 17:47
  • @user17732522, you should make that another answer. It adds value beyond what the existing answers already say. – Gabriel Staples May 24 '22 at 17:48