Boost logo

Boost Users :

From: Stephen Gross (sgross_at_[hidden])
Date: 2005-09-13 13:33:59


I've put together a class called 'AnyVector' for doing type-based iteration
on a vector. The basic idea is that AnyVector wraps a
std::vector<boost::any>. The AnyVector lets you iterate across a subset of
the underlying vector by using filter_iterators. The filter iterators, in
turn, dereference to the requested type for ease of use. For instance, you
can do the following:

=== CODE ===
AnyVector<> a;
a.push_back(1);
a.push_back(std::string("hello"));
a.push_back(2);
a.push_back(std::string("goodbye"));

for(AnyVector<>::Iterator<int> i = a.begin<int>(); i != a.end<int>(); ++i)
std::cout << (*i + 5);
for(AnyVector<>::Iterator<std::string> i = a.begin<std::string>(); i !=
a.end<std::string>(); ++i) std::cout << i->length();
============

Furthermore, you can restrict (at compile-time) which objects are allowed to
be inserted into the AnyVector. This is done by listing those types when you
create your AnyVector:

=== CODE ====
AnyVector<int, std::string> a; // Only allows int's and string's
a.push_back(3.5); // Fails at compile time - double's not allowed!
=============

Would this be a useful addition to boost, or does it already exist somewhere
in boost and I just missed it?

--Steve

Stephen Gross
Case Western School of Medicine
Cleveland, OH

"By Grabthar's hammer, by the sons of Worvan, you shall be avenged." - Dr.
Lazarus


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net