value_type_t<string[10]> // string
value_type<std::vector<const float *>>::type // const float *
value_type_t<std::list<array<char, 5>>> // array<char, 5>template<class T>
struct value_type;
template<class T, std::size_t N>
struct value_type<T[N]> {
using type = T;
};
template<class T>
struct value_type<std::vector<T>> {
using type = T;
};
template<class T>
struct value_type<std::list<T>> {
using type = T;
};
// and everything else for which there is begin(T)
template<typename T>
using value_type_t = typename value_type<T>::type;
template<typename T>
struct value_type
{
using type = decltype(*std::begin(*((T*)(NULL))));
};
int main()
{
cout << typeid(value_type<string[10]>::type).name() << endl;
cout << typeid(value_type<std::vector<const float *>>::type).name() << endl;
cout << typeid(value_type<std::list<std::array<char, 5>>>::type).name() << endl;
cout << typeid(string).name() << endl;
cout << typeid(const float *).name() << endl;
cout << typeid(std::array<char, 5>).name() << endl;
}
...output...
Ss
PKf
St5arrayIcLm5EE
Ss
PKf
St5arrayIcLm5EE
| From: Derek Ross Sent: Saturday, December 10, 2016 10:31 PM To: ISO C++ Standard - Future Proposals Reply To: std-pr...@isocpp.org Subject: [std-proposals] Re: value_type<T> |
Use declval<T>() instead of casting null.
template<class T>
struct value_type {
using type = typename std::remove_reference<decltype(*std::begin(declval<T&>()))>::type;
};std::vector<bool>This should be the same as
std::iterator_traits<decltype(begin(declval<T&>()))>::value_type
What type do you want it to return for? I'd rather get bool than the proxy.std::vector<bool>
Note that Boost calls this range_value.