0

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.

Update:

  1. 3.5 months after asking the original question, I was made aware of this question also: What does int() do in C++?
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • Is `char` considered a class? If not, this question is certainly _not_ a duplicate. Voting to re-open. – Gabriel Staples Sep 05 '22 at 17:17
  • I have added more accurate dupe: https://stackoverflow.com/questions/8860780/what-does-value-initializing-something-mean – Jason Sep 05 '22 at 17:18
  • Refer to [how to ask](https://stackoverflow.com/help/how-to-ask) where the first step is to *"search and then research"* and you'll find plenty of related SO posts for this. – Jason Sep 05 '22 at 17:18
  • @JasonLiam, I suggest you take your own advice, and prove you can find even _one_ related SO posts for this. Neither of the 2 duplicates show initialization of integer types via `int()`, `char()`, `uint64_t()`, etc., which is the whole crux of this question. And, they are both closed. – Gabriel Staples Sep 05 '22 at 17:23
  • 1
    Here is another dupe: [What does int() do in C++?](https://stackoverflow.com/questions/17131911/what-does-int-do-in-c) – Jason Sep 05 '22 at 17:24
  • *"prove you can find even one related SO posts for this..."* Here is the proof: [What does int() do in C++?](https://stackoverflow.com/questions/17131911/what-does-int-do-in-c) – Jason Sep 05 '22 at 17:27
  • @JasonLiam, that's a legitimate duplicate, and unlike the other "duplicates", is still an open question, which I think should be a *requirement* for being marked as a duplicate, so new answers can be added. Thank you. How did you find it though? Despite your suggestion to "just search", I did plenty of that, and it's hard to search for this topic, and Google searches like `char() in c++` weren't very productive. – Gabriel Staples Sep 05 '22 at 17:28
  • I typed the following on google search bar: [what does int() mean c++](https://www.google.com/search?q=what+does+int%28%29+mean+c%2B%2B&client=firefox-b-d&biw=1366&bih=627&sxsrf=ALiCzsaNtXlUisOHX6JcHhjFgzCFQAsC3w%3A1662398773337&ei=NTEWY4idFLamz7sPkKmOyAU&ved=0ahUKEwjIi_CClv75AhU203MBHZCUA1kQ4dUDCA0&uact=5&oq=what+does+int%28%29+mean+c%2B%2B&gs_lcp=Cgdnd3Mtd2l6EAMyBAgjECcyBAgjECcyBggAEB4QDTIGCAAQHhAWMgYIABAeEBYyBggAEB4QFjIICAAQHhAIEA0yBQgAEIYDMgUIABCGA0oECEEYAUoECEYYAFC6AVi_BmDjCGgBcAB4AIABgQKIAaMDkgEFMC4xLjGYAQCgAQHAAQE&sclient=gws-wiz). You're welcome :) – Jason Sep 05 '22 at 17:30
  • @JasonLiam, I think you got lucky by starting with `int()`. None of `what does char() mean c++`, `what does uint8_t() mean c++`, `what does int64_t() mean c++`, `what does double() mean c++`, `what does T() mean c++`, etc, produce similar useful results. – Gabriel Staples Sep 05 '22 at 17:33
  • @JasonLiam, I also think you were _way too fast_ in closing this without even having a true duplicate before you did so. And, I ask you to please slow down in clicking the "close" button in the future. It's insanely infuriating, especially when you didn't have an actual duplicate until your 3rd attempt. I was about to add a very thorough answer to this question myself, which is why I edited the question just now in the first place. – Gabriel Staples Sep 05 '22 at 17:36

2 Answers2

6

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.

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207
  • 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
  • See also: https://en.cppreference.com/w/cpp/language/zero_initialization. "Zero initialization" is not really a concept in C++, but it has a reference page because some other types of initialization, such as "value initialization", may initialize values to zero. Here is a quote from the link above: _"Note that this is not the syntax for zero-initialization, which does not have a dedicated syntax in the language. These are examples of other types of initializations, which might perform zero-initialization."_ – Gabriel Staples Sep 01 '22 at 04:03
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.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • 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) – Jason 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. – Jason 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