It is easy given a container to get the associated iterators, example:
std::vector<double>::iterator i; //An iterator to a std::vector<double>
I was wondering if it is possible, given an iterator type, to deduce the type of the "corresponding container" (here I am assuming that for each container there is one and only one (non-const) iterator).
More precisely, I would like a template metafunction that works with all STL containers (without having to specialize it manually for each single container) such that, for example:
ContainerOf< std::vector<double>::iterator >::type
evaluates to
std::vector<double>
Is it possible? If not, why?
Thank you in advance for any help!
Iterator type can be checked by using typeid.
5. prev() :- This function returns the new iterator that the iterator would point after decrementing the positions mentioned in its arguments. 6.
I don't think this would be possible. On some STL libraries you actually have a vector iterator as a pointer type, i.e. std::vector<T>::iterator is a T*
so I can't think of any way you could get back to the container type from that.
Just for fun, here's something I quickly hacked with Boost.MPL (warning: This was veeeery superficially tested, so handle with care):
#include <boost/mpl/list.hpp>
#include <boost/mpl/find_if.hpp>
#include <boost/type_traits.hpp>
#include <vector>
#include <string>
#include <list>
#include <set>
// List of candidate container types
template<typename T>
struct ContainersOf : boost::mpl::list<
std::vector<T>,
std::basic_string<T>,
std::list<T>,
std::set<T>
>{};
// Metafunction to evaluate if IteratorT == ContainerT::iterator
template<class IteratorT, class ContainerT>
struct IsIteratorOf
{
typedef typename
boost::is_same<
IteratorT,
typename ContainerT::iterator
>::type type;
};
// Metafunction to compute a container type from an iterator type
template<class IteratorT>
struct ContainerOf
{
typedef typename
boost::mpl::deref<typename
boost::mpl::find_if<
ContainersOf<typename std::iterator_traits<IteratorT>::value_type>,
IsIteratorOf<IteratorT, boost::mpl::_1>
>::type
>::type type;
};
// Test
int main()
{
ContainerOf<std::list<int>::iterator>::type l;
std::list<int> l2 = l; // OK
std::vector<int> v = l; // Fails to compile
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With