【问题标题】:How to use const auto with STLport library如何将 const auto 与 STLport 库一起使用
【发布时间】:2013-08-17 13:48:43
【问题描述】:

我尝试编译基本示例:

#include <vector>
int main ()
{
    std::vector<int> testV;
    for (const auto& test : testV) 
    {   }
    return 0;
}

我收到错误消息:
test.cpp: In function 'int main()':
test.cpp:5:29: error: 'begin' was not declared in this scope
test.cpp:5:29: error: 'end' was not declared in this scope
test.cpp:5:29: error: unable to deduce 'const auto&amp;' from '&lt;expression error&gt;'

STLport 支持const auto 吗?

编辑:我正在使用GCC 4.6
使用 4.7 及更高版本一切正常。

【问题讨论】:

  • 这取决于你的编译器,你使用的是哪个版本和什么编译器?
  • 实际上,我记得现在它首先查找 member 版本。然后是非会员(6.5.4)。所以它应该可以工作。
  • @TemplateRex 请查看标准的第 6.5.4 节。这似乎是一个很常见的误解,但首先查找的是成员版本。然后通过 ADL 的非成员版本(没有正常查找),std:: 被认为是关联的命名空间。 (pasted here,但格式错误)
  • @BoBTFish 该死的,你是对的,this simple test 确认它
  • @BoBTFish 这个误解至少部分是因为官方 C++11 之前的旧版本 C++0x 实际上只寻找非成员(但模板 std::beginstd::end 只是无论如何都要服从成员函数)。

标签: c++ c++11 auto stlport


【解决方案1】:

gcc 4.6 于 2011 年春天问世,在大多数 C++11 功能中并非没有错误。此外,大约在同一时间,range-for 中的 ADL 查找规则也被修改(请注意,这是在 2011 年夏天正式批准 C++11 标准之前)。有关详细信息,请参阅this Q&A。这可能不值得调试,推荐的做法是升级到最新版本的 gcc(4.7 或最好是 4.8)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    • 2018-05-13
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多