【发布时间】: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 scopetest.cpp:5:29: error: 'end' was not declared in this scopetest.cpp:5:29: error: unable to deduce 'const auto&' from '<expression error>'
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::begin和std::end只是无论如何都要服从成员函数)。