为什么std :: cbegin返回与std :: begin相同的类型 [英] Why does std::cbegin return the same type as std::begin

查看:186
本文介绍了为什么std :: cbegin返回与std :: begin相同的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cppreference 显示std::cbegin的此签名:

template< class C >
constexpr auto cbegin( const C& c ) -> decltype(std::begin(c));

它不应该返回类似C::const_iterator的内容吗?

解决方案

cconst引用,因此std::begin(c)它将返回C::begin()const重载返回的内容.对于标准库类型,这是const_iterator.对于数组类型,它是指向const的指针.

请注意,这依赖于其他非标准库用户定义的C,对C::begin()const重载是明智地实现的,该重载返回一个迭代器,该迭代器使您const可以访问容器的元素./p>

cppreference shows this signature for std::cbegin:

template< class C >
constexpr auto cbegin( const C& c ) -> decltype(std::begin(c));

Shouldn't it return something like C::const_iterator instead?

解决方案

c is a const reference, so std::begin(c) it will return whatever the const overload of C::begin() returns. For standard library types, this is a const_iterator. For an array type, it is a pointer to const.

Note that this relies on other, non-standard library user defined C, being implemented sanely with a const overload for C::begin() that returns an iterator that gives you const access to the container's elements.

这篇关于为什么std :: cbegin返回与std :: begin相同的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆