Decltype of a container from an argument of a function
c++, c++11, decltype
Solution
If you really really must force the use of `decltype` into this very simple code (and I'd call that misguided), then it's like this:
auto battery_capacity(const vector<double> & v) -> std::remove_reference<decltype(v)>::type::value_type
{
std::remove_reference<decltype(v)>::type::value_type b = 0;
return b;
}
If you're unsure why you need to write this, you really don't want to be using `decltype` at all. It's a very specialized tool for a very specialized kind of generic library writing, not for common household use.
Of course the simpler answer is just to write `double battery_capacity(...)`, since you know the type already! (If you don't actually know the type, then you didn't ask the right question, and your question doesn't reflect your actual problem.)
In C++14, you can be a little shorter:
auto battery_capacity(const vector<double> & v)
{
return std::remove_reference_t<decltype(v)>::value_type { 0 };
}
Problem
I'm trying to deduct the type of the stl container inside a function from the function's argument and it doesn't compile: ``` auto battery_capacity( const vector<double>& v) -> decltype(v)::value_type { decltype(v)::value_type b=0; return b; } ``` error: ``` main.cpp:10:52: error: 'decltype(v)' (aka 'const vector<double> &') is not a class, namespace, or scoped enumeration auto battery_capacity( const vector<double>& v) -> decltype(v)::value_type { ^ main.cpp:11:3: error: 'decltype(v)' (aka 'const vector<double> &') is not a class, namespace, or scoped enumeration decltype(v)::value_type b=0; ``` however this compiles: ``` double battery_capacity( const vector<double>& v) { vector<double> s=v; decltype(s)::value_type b=0; return b; } ``` Update (1) Addition for when using templates based on Kerrek's answer: ``` template <typename T> auto battery_capacity( const T& v) -> typename std::remove_reference<decltype(v)>::type::value_type { typename std::remove_reference<decltype(v)>::type::value_type b=0; return b; } ```