Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access elements in std::string where positon of string is greater than its size

In case of std::string, if we access an element where (element position) == (size of string) the standard says that it returns a reference to an object of type charT with value charT().

const_reference operator[](size_type pos) const; reference       operator[](size_type pos); 

Expects: pos <= size().

Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.

http://eel.is/c++draft/strings#string.access-1

Unfortunately I couldn't reason about this, it would have been better if it has been Undefined Behavior.

Can somebody explain the rationale behind this?

like image 578
cpp_enthusiast Avatar asked Apr 09 '19 08:04

cpp_enthusiast


People also ask

How do I use sub str in C++?

Substring in C++ A substring is a part of a string. A function to obtain a substring in C++ is substr(). This function contains two parameters: pos and len. The pos parameter specifies the start position of the substring and len denotes the number of characters in a substring.

Does std :: string store size?

std::string actually maintains the size as one of its data member.

How do you access the elements of a string?

String Indexing Individual characters in a string can be accessed by specifying the string name followed by a number in square brackets ( [] ). String indexing in Python is zero-based: the first character in the string has index 0 , the next has index 1 , and so on.

Is substring inclusive C++?

A substring is a sequence of consecutive characters from a string. For example, “with Educative” is a substring of “Learn C++ with Educative”. The function will return a substring from the character of the pos index (inclusive) up to the character with the ( pos + len - 1) index.


1 Answers

You have to consider the full specs.

First of all:

Expects: pos <= size().

If you dont follow the precondition you have undefined behaviour anyhow. Now...

Returns: *(begin() + pos) if pos < size(). Otherwise, returns a reference to an object of type charT with value charT(), where modifying the object to any value other than charT() leads to undefined behavior.

The only (valid) case that "otherwise" refers to is when pos == size(). And that is probably to emulate c string behaviour that have a some_string[size] element that can be accessed. Note that charT() is typically just '\0'.

PS: One might think that to implement the specification, operator[] would have to check if pos == size. However, if the underlying character array has a charT() at the end of the string, then you get the described behaviour basically for free. Hence, what seems a little different from "usual" access into an array is actually just that.

like image 79
463035818_is_not_a_number Avatar answered Oct 06 '22 14:10

463035818_is_not_a_number