为什么std :: cbegin返回与std :: begin相同的类型 [英] Why does std::cbegin return the same type as std::begin
问题描述
cppreference 显示std::cbegin
的此签名:
template< class C >
constexpr auto cbegin( const C& c ) -> decltype(std::begin(c));
它不应该返回类似C::const_iterator
的内容吗?
c
是const
引用,因此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屋!