Skip to main content
added 1 character in body
Source Link
Daniel Kamil Kozar
  • 19.6k
  • 5
  • 54
  • 71

It is fine since C++11.

Before C++11, there was no formal guarantee that operator[] would return a reference to a character that would be part of a null-terminated character array (i.e. a C-style string). One consequence of that missing guarantee was that &b[0u] would have been undefined behaviour if b was an empty non-const string.

(Actual implementations typically behaved correctly anyway, because that's the only saysane way of implementing std::string, but that's another story.)

See also http://en.cppreference.com/w/cpp/string/basic_string/operator_at:

reference       operator[]( size_type pos );

(...)

If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(since C++11)

Still, the code you've posted is not particularly good style. Why create a pointer with an uninitialised value and then assign it a value later on, and why bother with the more complicated syntax?

Here's an improved version of the code:

std::string b;
std::getline(std::cin, b);
auto const a = b.c_str();

In this version, a is const, so you cannot accidentally make it point to something else; you also make the compiler deduce the type (char const*) automatically. And c_str() is a clearer way of saying what your code actually means.

It is fine since C++11.

Before C++11, there was no formal guarantee that operator[] would return a reference to a character that would be part of a null-terminated character array (i.e. a C-style string). One consequence of that missing guarantee was that &b[0u] would have been undefined behaviour if b was an empty non-const string.

(Actual implementations typically behaved correctly anyway, because that's the only say way of implementing std::string, but that's another story.)

See also http://en.cppreference.com/w/cpp/string/basic_string/operator_at:

reference       operator[]( size_type pos );

(...)

If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(since C++11)

Still, the code you've posted is not particularly good style. Why create a pointer with an uninitialised value and then assign it a value later on, and why bother with the more complicated syntax?

Here's an improved version of the code:

std::string b;
std::getline(std::cin, b);
auto const a = b.c_str();

In this version, a is const, so you cannot accidentally make it point to something else; you also make the compiler deduce the type (char const*) automatically. And c_str() is a clearer way of saying what your code actually means.

It is fine since C++11.

Before C++11, there was no formal guarantee that operator[] would return a reference to a character that would be part of a null-terminated character array (i.e. a C-style string). One consequence of that missing guarantee was that &b[0u] would have been undefined behaviour if b was an empty non-const string.

(Actual implementations typically behaved correctly anyway, because that's the only sane way of implementing std::string, but that's another story.)

See also http://en.cppreference.com/w/cpp/string/basic_string/operator_at:

reference       operator[]( size_type pos );

(...)

If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(since C++11)

Still, the code you've posted is not particularly good style. Why create a pointer with an uninitialised value and then assign it a value later on, and why bother with the more complicated syntax?

Here's an improved version of the code:

std::string b;
std::getline(std::cin, b);
auto const a = b.c_str();

In this version, a is const, so you cannot accidentally make it point to something else; you also make the compiler deduce the type (char const*) automatically. And c_str() is a clearer way of saying what your code actually means.

Source Link
Christian Hackl
  • 27.7k
  • 4
  • 36
  • 62

It is fine since C++11.

Before C++11, there was no formal guarantee that operator[] would return a reference to a character that would be part of a null-terminated character array (i.e. a C-style string). One consequence of that missing guarantee was that &b[0u] would have been undefined behaviour if b was an empty non-const string.

(Actual implementations typically behaved correctly anyway, because that's the only say way of implementing std::string, but that's another story.)

See also http://en.cppreference.com/w/cpp/string/basic_string/operator_at:

reference       operator[]( size_type pos );

(...)

If pos == size(), a reference to the character with value CharT() (the null character) is returned.

(since C++11)

Still, the code you've posted is not particularly good style. Why create a pointer with an uninitialised value and then assign it a value later on, and why bother with the more complicated syntax?

Here's an improved version of the code:

std::string b;
std::getline(std::cin, b);
auto const a = b.c_str();

In this version, a is const, so you cannot accidentally make it point to something else; you also make the compiler deduce the type (char const*) automatically. And c_str() is a clearer way of saying what your code actually means.