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++?
10
  • Is char considered a class? If not, this question is certainly not a duplicate. Voting to re-open. Commented Sep 5, 2022 at 17:17
  • I have added more accurate dupe: stackoverflow.com/questions/8860780/… Commented Sep 5, 2022 at 17:18
  • Refer to how to ask where the first step is to "search and then research" and you'll find plenty of related SO posts for this. Commented Sep 5, 2022 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. Commented Sep 5, 2022 at 17:23
  • 1
    Here is another dupe: What does int() do in C++? Commented Sep 5, 2022 at 17:24

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! For anyone else wondering, I found the cppreference community wiki documentation for value initialization here: en.cppreference.com/w/cpp/language/value_initialization
See also: 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."
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.

10 Comments

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?
It's not a class, so it doesn't have constructors. Pardon my nitpicking.
char foo(); is most-vexing-parse.
@RichardCritten It's a vexing parse alright, but not the most vexing parse. :-)
@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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.