7

I have been using std::rbegin and std::rend in MSVC 2013. When I tried to compile my code using GCC 4.9.1 or clang 3.5.0, both tell me that 'rbegin' and 'rend' are not part of namespace 'std'.

See the code example below. Am I doing something wrong or are they simply not yet implemented in GCC and clang?

// test.cpp

#include <vector>
#include <iostream>
#include <iterator>

int main(int, char**)
{
    std::vector<int> test = {1, 2, 3 ,4, 5};
    for (auto it = std::rbegin(test); it != std::rend(test); ++it) {
        std::cout << *it << ", ";
    }
    std::cout << std::endl;

    return 0;
}

GCC output:

g++ --std=c++14 test.cpp -o test && ./test
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10:20: error: ‘rbegin’ is not a member of ‘std’
     for (auto it = std::rbegin(test); it != std::rend(test); ++it) {
                    ^
test.cpp:10:45: error: ‘rend’ is not a member of ‘std’
     for (auto it = std::rbegin(test); it != std::rend(test); ++it) {
                                             ^

clang output is similar, generated with:

clang++ --std=c++14 test.cpp -o test && ./test
Peter K
  • 1,372
  • 8
  • 24

1 Answers1

5

It does work with Clang 3.5 using the -std=c++14 -stdlib=libc++ option. See this Live Example. I think the libstdc++ library support for rbegin() and rend() is not yet complete as version 4.9.2 (and it is also not yet implemented in the upcoming gcc 5.0 release).

UPDATE: it now works in the gcc 5.0 trunk release.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • Right, so it's just libstdc++ that doesn't have it yet. – Lightness Races in Orbit Jan 13 '15 at 12:30
  • 2
    [According to the documentation](http://en.cppreference.com/w/cpp/iterator/rbegin) it is a new C++14 feature. I'm still surprised it hasn't been implemented yet though. – sjdowling Jan 13 '15 at 12:31
  • @LightnessRacesinOrbit I think the upcoming gcc 5 will have it. Haven't found an online compiler to demonstrate that. – TemplateRex Jan 13 '15 at 12:31
  • 1
    @TemplateRex [Wandbox](http://melpon.org/wandbox/). Although as of 1/12/2015, it doesn't appear to be implemented. –  Jan 13 '15 at 13:14
  • 1
    The [gcc 5 changes page](https://gcc.gnu.org/gcc-5/changes.html#libstdcxx) claims to have it in the next libstdc++: "*global functions cbegin, cend, rbegin, rend, crbegin, and crend for range access to containers, arrays and initializer lists.*" Running the code in your final Wandbox now works. – Ryan Haining Apr 17 '15 at 21:15