What is a call to `char()`, `uint8_t()`, `int64_t()`, integer `T()`, etc, as a function in C++? [duplicate]
Asked Answered
P

2

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++?
Phippen answered 24/5, 2022 at 17:31 Comment(10)
Is char considered a class? If not, this question is certainly not a duplicate. Voting to re-open.Phippen
I have added more accurate dupe: #8861280Odrick
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.Odrick
@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.Phippen
Here is another dupe: What does int() do in C++?Odrick
"prove you can find even one related SO posts for this..." Here is the proof: What does int() do in C++?Odrick
@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.Phippen
I typed the following on google search bar: what does int() mean c++. You're welcome :)Odrick
@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.Phippen
@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.Phippen
A
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.

Amperage answered 24/5, 2022 at 17:33 Comment(10)
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?Phippen
It's not a class, so it doesn't have constructors. Pardon my nitpicking.Ambler
char foo(); is most-vexing-parse.Emperor
@GabrielStaples: Yep, int() is 0, uint64_t() is UINT64_C(0), etc.Amperage
@RichardCritten Apparantely char foo(); is not considered most-vexing parse. See Is most vexing parse a formally defined conceptOdrick
@AnoopRana clang/gcc beg to differ - live - godbolt.org/z/PdhEMKqr3Emperor
@RichardCritten It's a vexing parse alright, but not the most vexing parse. :-)Crossfertilize
@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.Anthracene
@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, 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.Odrick
@user17732522, you should make that another answer. It adds value beyond what the existing answers already say.Phippen
A
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.

Ambler answered 24/5, 2022 at 17:33 Comment(2)
Thanks! For anyone else wondering, I found the cppreference community wiki documentation for value initialization here: en.cppreference.com/w/cpp/language/value_initializationPhippen
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."Phippen
A
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.

Amperage answered 24/5, 2022 at 17:33 Comment(10)
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?Phippen
It's not a class, so it doesn't have constructors. Pardon my nitpicking.Ambler
char foo(); is most-vexing-parse.Emperor
@GabrielStaples: Yep, int() is 0, uint64_t() is UINT64_C(0), etc.Amperage
@RichardCritten Apparantely char foo(); is not considered most-vexing parse. See Is most vexing parse a formally defined conceptOdrick
@AnoopRana clang/gcc beg to differ - live - godbolt.org/z/PdhEMKqr3Emperor
@RichardCritten It's a vexing parse alright, but not the most vexing parse. :-)Crossfertilize
@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.Anthracene
@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, 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.Odrick
@user17732522, you should make that another answer. It adds value beyond what the existing answers already say.Phippen

© 2022 - 2024 — McMap. All rights reserved.