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

雨燕双飞 提交于 2020-01-03 13:35:45

问题


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.


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/31038061/support-of-stdcbegin-in-c14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!