Support of std::cbegin() in C++14

1.1k views Asked by At

Item 13 from Scott Mayers' "Effective Modern C++" states to prefer const_iterators over iterators. I agree but I also want to use non-member functions rather than member functions. According to the book there should be a non-member function std::cbegin() and std::cend() in C++14.

To make use of this functions I just installed gcc version 4.9.2 and compiled with the flag -std=c++14. It seems to compile until I try to use std::cbegin(). I start searching for the support for this function but couldn't find anything about it. For example, at gnu onlinedocs status the function isn't even mentioned.

My question is, will std::cbegin() and std::cend() really be supported in c++14 or is this a mistake in the book? If it will be a C++14 feature, are there compilers which already support these functions and when will gcc support it?

There are many questions at SO about begin() but these questions are about the member functions or about the constexpr-ness and not about the support of the non-member variant.

3

There are 3 answers

0
Tavian Barnes On BEST ANSWER

GCC 4.9's support for C++14 is experimental and incomplete. But here, you can see that

global functions cbegin, cend, rbegin, rend, crbegin, and crend for range access to containers, arrays and initializer lists.

were added in GCC 5.1.

1
T.C. On

Yes, they are in C++14. They were added by a library issue, not by a paper, and it looks like libstdc++'s manual page doesn't track library issues.

They are implemented in GCC 5.1. See GCC bug 64656.

0
luk32 On

Yes they are, cppreference describes them along begin/end.

And the standard defines it in section 24.7 - Range access. I'm not citing it because it's a bunch of template function definitions which agree with the above links.

The draft, which should be very close to the final version (I can't remember the number for the final draft, sorry): http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf

As a matter of fact, you can play with embedded examples on the cppreference, and change begin and end calls to cbegin/cend and try to run them. gcc 4.9 complains, though gcc 5.1 already works, as well as clang 3.6. So it's just a matter of compiler support.