GCC 4.9和clang 3.5中的std :: rbegin和std :: rend函数

时间:2015-01-13 12:24:32

标签: c++ gcc iterator clang c++14

我一直在MSVC 2013中使用std :: rbegin和std :: rend。当我尝试使用GCC 4.9.1或clang 3.5.0编译我的代码时,两者都告诉我'rbegin'和'rend'是不属于命名空间'std'。

请参阅下面的代码示例。我做错了什么还是他们还没有在GCC和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输出:

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输出类似,使用:

生成
clang++ --std=c++14 test.cpp -o test && ./test

1 个答案:

答案 0 :(得分:5)

使用-std=c++14 -stdlib=libc++选项与Clang 3.5配合使用。见Live Example。我认为对于rbegin()rend()的libstdc ++库支持尚未完成,因为版本4.9.2(它也是not yet implemented in the upcoming gcc 5.0版本)。

UPDATE :它现在可以在gcc 5.0 trunk版本中使用。