在 C++11 标准中是否指定 std::begin(Container&&) 返回const_iterator?
Is it specified in the C++11 standard that std::begin(Container&&) returns const_iterator?
这里有一个相关代码的链接:
#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
int main()
{
std::vector<int> v{1, 2, 3, 4, 5};
auto iter = begin(std::move(v));
if(std::is_const<typename std::remove_reference<decltype(*iter)>::type>::value)
std::cout<<"is constn";
return 0;
}
http://coliru.stacked-crooked.com/a/253c6373befe8e50
我遇到这种行为是因为decltype
表达式中的declval<Container>()
与std::begin
.gcc 和 clang 都返回迭代器,这些迭代器在取消引用时会产生常量引用。这可能是有意义的,因为 r 值引用通常绑定到您不想改变的过期对象。但是,我找不到任何有关此的文档来确定它是否是标准规定的。我找不到任何相关的begin()
重载或 ref 合格的重载Container::begin()
.
更新:答案阐明了正在发生的事情,但交互可能很微妙,如下所示:
#include <iostream>
#include <string>
#include <vector>
#include <type_traits>
int main()
{
if(std::is_const<typename std::remove_reference<decltype(*begin(std::declval<std::vector<std::string>>()))>::type>::value)
std::cout<<"(a) is constn";
if(!std::is_const<typename std::remove_reference<decltype(*std::declval<std::vector<std::string>>().begin())>::type>::value)
std::cout<<"(b) is not constn";
if(!std::is_const<typename std::remove_reference<decltype(*begin(std::declval<std::vector<std::string>&>()))>::type>::value)
std::cout<<"(c) is not constn";
return 0;
}
http://coliru.stacked-crooked.com/a/15c17b288f8d69bd
天真地,当 ::begin 只是根据调用 vector::start 来定义时,你不会期望 (a( 和 (b( 有不同的结果。但是,缺少接受非常量 r 值引用并返回迭代器的 std::start 重载(或返回 const_iterator 的 ref 限定的 vector::begin 重载(会导致这种情况发生。
正如您在 http://en.cppreference.com/w/cpp/iterator/begin 中看到的,有趣的重载是:
template<class C> auto begin(C& c) -> decltype(c.begin());
template<class C> auto begin(const C& c) -> decltype(c.begin());
并且std::vector<int>&&
只能绑定到第二个重载(因此返回const_iterator
(。
让我们尝试逐步分析会发生什么:
-
您正在调用
std::begin(std::vector<int>&&)
,但std::begin
没有采用右值的重载:template< class C > auto begin( C& c ) -> decltype(c.begin()); template< class C > auto begin( const C& c ) -> decltype(c.begin());
-
由于引用折叠,临时 (xvalue( 将仅绑定到
const
左值引用:如果你用xvalue调用Fwd,我们再次得到Type&&作为v的类型。这将不允许您调用采用非常量左值的函数,因为 xvalue 无法绑定到非常量左值引用。它可以绑定到一个 const lvalue 引用,所以如果 Call 使用 const&,我们可以用 xvalue 调用 Fwd。
(来自链接的答案(。
-
因此,
template<class C> auto begin(const C& c) -> decltype(c.begin());
正在调用重载,这将返回一个
const
迭代器。为什么?
因为
std::begin(v)
调用v.begin()
,所以在std::vector
的const
实例上调用时会返回一个const_iterator
。
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 模板化函数以从输入参数推断返回类型 stl-container
- 使用 "typename Container::value_type" 作为返回值时无法推断模板参数
- 在 C++11 标准中是否指定 std::begin(Container&&) 返回const_iterator?
- 为什么来自 rvalued-stl-container 的元素获取者返回左值