Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 898
Michiel uit het Broek Avatar asked Jun 24 '15 22:06

Michiel uit het Broek


3 Answers

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.

like image 197
Tavian Barnes Avatar answered Nov 07 '22 01:11

Tavian Barnes


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.

like image 27
T.C. Avatar answered Nov 07 '22 03:11

T.C.


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.

like image 20
luk32 Avatar answered Nov 07 '22 02:11

luk32