Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why can't the std::string constructor take just a char?

While using a function template, I was looking at how to convert a char into a length 1 string in C++, and I saw that std::string(1, c) converted the char into a string using the fill constructor, following the logic of "repeat char c 1 time to form a string." However, there is no constructor overload defined for std::string that takes in just a char and turns it into a std::string.

Why is this, and what was the design decision behind not including this constructor? It feels like a very natural and intuitive idea to be able to convert a char into a string directly rather than having to use a fill constructor.

My goal is to be able to use chars and std::strings uniformly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In C and C++, char serves double-duty. It represents both a character and a number. It is considered an integral type, which means that it participates in implicit integer promotion to many other integral types, as well as implicit conversion from other integral types.

Because char is overloaded, it is impossible at the level of a function’s interface to know if a user passed in an actual character (a character literal or a variable of type char) or a numeric literal that just so happens to be small enough to fit into a char.

As such, when using char in an interface, one must be careful of accidental conflicts with what the user is providing.

Sequence container types (types that hold a number of elements in a sequence unrelated to the values of those elements) usually have a constructor that takes a count of Ts. This is the number of elements to create in the sequence, constructed via value initialization (there is also has a version that takes a T which is used to copy-initialize these elements, which is what you used).

The size is an integral type. So if the user uses the integer literal 4, it will be promoted to that type, usually std::size_t. But 4 could also be converted into a char with no loss of data. So if there were another constructor that took only a char, there would be an overload resolution problem. If you passed a literal char (like 'c'), there wouldn’t be a conflict, but integer literals do conflict.

And thus, you wouldn’t be able to initialize a string with std::string s(4);.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading