【问题标题】:std::rbegin and std::rend function in GCC 4.9 and clang 3.5GCC 4.9 和 clang 3.5 中的 std::rbegin 和 std::rend 函数
【发布时间】:2015-01-13 12:24:32
【问题描述】:

我一直在 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

【问题讨论】:

  • 您正在比较 GCC 和 Clang,但默认情况下两者都使用 libstdc++,这确实是您应该比较的标准库实现。

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


【解决方案1】:

它确实适用于使用 -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 版本)。

更新:它现在可以在 gcc 5.0 主干版本中使用。

【讨论】:

  • 对,只是libstdc++还没有。
  • According to the documentation 这是一个新的 C++14 特性。我仍然对它尚未实施感到惊讶。
  • @LightnessRacesinOrbit 我认为即将推出的 gcc 5 将拥有它。还没有找到一个在线编译器来证明这一点。
  • @TemplateRex Wandbox。尽管截至 2015 年 1 月 12 日,它似乎还没有实施。
  • gcc 5 changes page 声称在下一个 libstdc++ 中有它:“全局函数 cbegin、cend、rbegin、rend、crbegin 和 crend 用于对容器、数组和初始化列表的范围访问。 " 现在可以在最终的 Wandbox 中运行代码了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-10
  • 2019-06-06
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 1970-01-01
  • 2020-09-05
相关资源
最近更新 更多