Talk:cpp/string/basic string/front
From cppreference.com
Why not reference
Hi. Maybe this is not the proper place to ask, but does anybody know why the interface of std::string::front and std::string::back uses, as return type, the plain (const)references instead of these dedicated types:
as opposed to the interface of, e.g., std::string::at. Is this divergence on purpose?
An excerpt from <string>:
namespace std {
template<class CharT, class Traits = char_traits<CharT>,
class Allocator = allocator<CharT>>
class basic_string {
public:
// types
// ...
using value_type = CharT;
// ...
using reference = value_type&;
using const_reference = const value_type&;
// ...
// element access
constexpr const_reference operator[](size_type pos) const; // OK - const_reference
constexpr reference operator[](size_type pos); // OK - reference
constexpr const_reference at(size_type n) const; // OK
constexpr reference at(size_type n); // OK
constexpr const CharT& front() const; /* My: Wait, what? Not `const_reference`? */
constexpr CharT& front(); /* My: ditto, `reference`? */
constexpr const CharT& back() const; /* My: ditto, `const_reference`? */
constexpr CharT& back(); /* My: ditto, `reference`? */
In the better imaginary world it should be:
> constexpr const_reference front() const; // happiness
constexpr reference front(); // happiness
constexpr const_reference back() const; // happiness
constexpr reference back(); // happiness
Most likely, this is also connected with LWG issue 534.
--Space Mission (talk) 10:26, 27 March 2023 (PDT)
- There's no reason to use or to avoid the
referencealiases, it's just an editorial inconsistency. Note that libstdc++, libc++ and MS stdlib all usereference. If you were to make them consistent here on cppreference, more power to you --Ybab321 (talk) 11:00, 27 March 2023 (PDT)
- Yea, this was my attempt to limelight this inconsistency. Should it be DR (too much for such a smidge change) or a letter to Thomas? I just would prefer to see this is fixed in draft/ISO first, then reflect it here.) --Space Mission (talk) 11:25, 27 March 2023 (PDT)
- Editorial issues are raised on the github --Ybab321 (talk) 11:44, 27 March 2023 (PDT)
CharT&andconst CharT&are shorter and clearer. If the standard wording changes to sayreferenceandconst_reference, I won't follow. --Fruderica (talk) 04:24, 10 January 2024 (PST)- I've opened editorial PR #6764 for this. --Fruderica (talk) 05:23, 10 January 2024 (PST)
- Hi there.)
- I skimmed:
- #6764
- the standard headers, and
- some implementations of the standard library,
- and this is what I found:
- All sequence-like container-like types std::array, std::list, std::forward_list, std::deque, std::queue,
std::span, std::stack (fortop()), std::vector, std::bitset usereference/const_referencefor element access interfaceat()/[]/front()/back(). - Only std::valarray uses (maybe
const)T&. - Associative containers on the other hand prefer (maybe
const)Container::mapped_type&.
- All sequence-like container-like types std::array, std::list, std::forward_list, std::deque, std::queue,
- But more importantly, all four vendors already use
reference/const_referenceas return types ofat()/[]/front()/back()element access members of std::string and std::string_view!- clang's libc++ does,
- gcc's libstdc++ does in string, string_view and even in
ext::vstring.) Only their std::experimental::string_view (as of gcc-13.2) still uses (maybeconst)_CharT&.) - OpenSTL (msvc) also uses
reference/const_reference(their "element type" is named_Elem). - ESTL also uses
reference/const_referencein their string and string_view.
- So
reference/const_referenceis preferable for the following reasons:- standardization of existing practice,
- independence from the template parameter name (which is obviously IRL, not
CharT, and varies from vendor to vendor), - consistency with existing "container-like" interfaces,
- etc...
- --Space Mission (talk) 11:02, 12 January 2024 (PST)