根据下面的代码,我如何通过头文件确认 v[0]
的类型是 const int&
?
#include <vector>
int main()
{
const std::vector<int> v(1);
decltype(v[0]) b = 1;
}
例如,这就是我在 VS2010 中尝试做的事情:
将鼠标悬停在上面的
vector
上,我用鼠标右键单击并选择了Go to definition
我找到了template<class _Ty, class _Ax = allocator<_Ty> > class vector : public _Vector_val<_Ty, _Ax> {}
然后我搜索了
operator[]
并找到了const_reference operator[](size_type _Pos) const
- 再次将鼠标悬停在
const-reference
上,我单击鼠标右键并选择Go to definition
并找到typedef typename _Alloc::const_reference const_reference;
。
从现在开始我不知道该去哪里。
最佳答案
你真的不应该使用实现来获取这样的信息。 typedef 链可能很长。而是引用语言标准或 cppreference.com反射(reflect)了标准中的信息。在std::vector
的情况下const_reference
被定义为对 value_type
类型常量的引用,它是类型 T
(或 _Ty
MS 实现)。
*.在 C++11 之前,它被声明为对 Allocator::reference
类型常量的引用
关于c++ - 鉴于下面的代码片段,我如何确认 v[0] 解析 VS2010 中的 <vector> 头文件的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16764909/