简体   繁体   中英

Given the snippet below how can I confirm the type of v[0] parsing the <vector> header file in VS2010?

Given the code below how can I confirm that the type of v[0] is const int& , going through the header file?

#include <vector>

int main()
{
    const std::vector<int> v(1);
    decltype(v[0]) b = 1;
}

As an example, that's what I was trying to do in VS2010:

  1. With the mouse over vector above, I clicked with the mouse right button and selected Go to definition and I found

     template<class _Ty, class _Ax = allocator<_Ty> > class vector : public _Vector_val<_Ty, _Ax> {} 
  2. I then searched for operator[] and found const_reference operator[](size_type _Pos) const

  3. Again with the mouse over const-reference I clicked with the mouse right button and selected Go to definition and found typedef typename _Alloc::const_reference const_reference; .

I don't know where to go from this point on.

You really shouldn't use the implementation to acquire information like this. The chain of typedefs can be quite long. Instead refer to either the language standard or cppreference.com which reflects the information in the standard. In the case of std::vector a const_reference is defined to be reference to a constant of type value_type which is a type T (or _Ty for the MS implementation).

*. Prior to C++11 it was declared as a reference to a constant of type Allocator::reference

Use type_traits and static_assert. std::is_same can directly tell you if it's const int& or not. For other cases you may use is_reference or other queries. static_assert is evaluated by intellisense, so you can even get the result without compile.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM