Suppose I have a vector like this:
std::vector<int> vec;
Then I want to know the type of vec
's elements. How can I get it? Too bad decltype(vec[0])
results in int&
.
Suppose I have a vector like this:
std::vector<int> vec;
Then I want to know the type of vec
's elements. How can I get it? Too bad decltype(vec[0])
results in int&
.
Is this what you're looking for?
std::vector<int>::value_type
You can also use
std::remove_reference<decltype(vec[0])>::type
to get rid of the reference.
Another option is to use decltype(vec)::value_type
. However, this doesn't currently work on Visual Studio due to a compiler bug. A workaround for that compiler is to create an intermediate typedef
.
typedef decltype(vec) vec_type;
vec_type::value_type foo;
For object, c
, of any container type including arrays and all standard library containers such as std::vector
and std::list
:
typename std::remove_reference<decltype(*std::begin(c))>::type