Is it nice to depend on index 0 of an empty std :: string?

std::string my_string = ""; char test = my_string[0]; 

I noticed that this is not a failure, and every time I tested it, the test is 0.

Can I always depend on it 0? or is it arbitrary?

Is this bad programming?

Edit: From some comments, I understand that there are some misunderstandings regarding the usefulness of this.

The purpose of this is NOT to check if the string is empty. No need to check if the string is empty.

The situation is that there is a line that may or may not be empty. I'm only interested in the first character of this line (if it's not empty).

It seems to me that it would be less efficient to check if a string is empty, and then if it is not empty, look at the first character.

 if (! my_string.empty()) test = my_string[0]; else test = 0; 

Instead, I can just look at the first character without having to check if the string is empty.

 test = my_string[0]; 
+43
c ++ stdstring
Oct 12 '15 at 14:06
source share
2 answers

C ++ 14

No; you can depend on him.

In 21.4.5.2 (or [string.access]) we can find:

Returns: *(begin() + pos) if pos < size() . Otherwise, a reference is returned to an object of type charT with the value charT() , where changing the object leads to undefined behavior.

In other words, if pos == size() (which is true when both are 0), the operator will return a link to the default string character , which you are forbidden to change .

It is not special for empty (or 0-bit) strings and works the same for each length.




C ++ 03

And, of course, C ++ 98.

It depends.

Here is 21.3.4.1 from official ISO / IEC 14882:

Returns: If pos < size() , returns data()[pos] . Otherwise, if pos == size() , the const version returns charT() . Otherwise, the behavior is undefined.

+67
Oct 12 '15 at 14:12
source share

@Bartek Banachewicz's answer explains what circumstances allow you to make your guess. I would like to add that

this is bad programming.

Why? For several reasons:

  • You must be a language lawyer to ensure that this is not a mistake. I would not know the answer, if not for this page, and honestly, I do not think that you really needed to know this.
  • People who do not have the intuition of a string, which is a sequence of characters with zero completion, will not know what you are trying to do until you read the standard or ask your friends.
  • Interrupts the principle of least surprise in a bad way.
  • It contradicts the principle of "write what you mean", that is, have code that expresses the concept of the problem area.
  • Sort the magic number (in this case, it can be argued that 0 really is a magic number).

Continue? ... I'm 99% sure that you have an alternative boss in almost every way. I am even 66% convinced that you did something else that is "bad" to manipulate yourself, wanting to do it.

Always remember: Other people who will not advise you will have to support this code sooner or later. Think about them, not yourself, who can understand this. Also, after ten years, who will say that you will remember your trick? Perhaps it was a confused attendant ...

+29
12 Oct '15 at 19:00
source share



All Articles