Here is part of my code:
int getLength(const vector<int> &arr) {
auto n=arr.size(),dis=n;
unordered_map<int,decltype(dis)> S;
//...
}
So far so good. Now instead of hardcoded "int
" for my std::unordered_map
, I tried to change it to something like:
unordered_map<decltype(arr.front()),decltype(dis)> S;
or
unordered_map<decltype(arr)::value_type,decltype(dis)> S;
or
unordered_map<decltype(arr[0]),decltype(dis)> S;
None seems to work. What would be the right grammar to use decltype()
here?
What would be the right grammar to use decltype here?
decltype(arr.front())
and decltype(arr[0])
are all OK but, unfortunately, all of they returning a reference to a const int
(considering that arr
is a constant vector)
You have to remove the reference and the const so, by example
std::unordered_map<
std::remove_const_t<std::remove_reference_t<decltype(arr.front())>>,
decltype(dis)> S;
Using the ::value_type
is better (IMHO) because you avoid the constness, so you have to remove only the reference, so you can write
std::unordered_map<
std::remove_reference_t<decltype(arr)>::value_type,
decltype(dis)> S;