Question

Is there any specific reason why there is no empty char literal?

What comes closest to what I think of, the '' is the '\0' the null character.

In C++ the char is represented by an int, which means empty char goes directly to the 0 integer value, which is in C++ "the same as null".

The practical part of coming up with that question:

In a class I want to represent char values as enum attributes. Unbiased I tried to initialize an instance with '', which of course does not work. But shouldn't be there a char null value? Not to be confused with string.Empty, more in the nature of a null reference.

So the question is: Why is there no empty char?

-edit-

Seeing this question the question can be enhanced on: An empty char value would enable concatening strings and chars without destroying the string. Would that not be preferable? Or should this "just work as expected"?

Was it helpful?

Solution 2

To give a slightly more technical explanation: There is no character that can serve as the identity element when performing concatenation. This is different from integers, where 0 serves as the identity element for addition.

OTHER TIPS

A char by definition has a length of one character. Empty simply doesn't fit the bill.

Don't run into confusion between a char and a string of max length 1. They sure look similar, but are very different beasts.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top