I have the following function which creates a std::vector of iterators into another container:
template <typename T,
template <typename, typename = std::allocator<T>> class Con>
std::vector<typename Con<T>::iterator> make_itervec(Con<T>& v)
{
std::vector<typename Con<T>::iterator> itervec;
for (auto i = v.begin(); i != v.end(); ++i)
{
itervec.push_back(i);
}
return itervec;
}
What I want to do is this:
template <typename T,
template <typename, typename = std::allocator<T>> class Con>
auto make_itervec(Con<T>& v) -> decltype(x) // This line
{
std::vector<typename Con<T>::iterator> itervec;
for (auto i = v.begin(); i != v.end(); ++i)
{
itervec.push_back(i);
}
return itervec;
}
What do I put for x to get this to work?
Tried but failed attempts:
decltype(std::vector<typename Con<T>::iterator>)
decltype(std::vector<decltype(v)::iterator>)
(Also, I’m not an expert on this, so any other suggestions, comments are welcome! Thanks.)
You could use
std::vector<decltype(v)::iterator>orstd::vector<decltype(v.begin())>. Note that there’s nodecltypearound thestd::vectorbecause that one already is a type, not a variable or expression.decltypeis used only to get the type of an expression.