The STL algorithms are a pretty useful thing in C++. But one thing that kind of irks me is that they seem to lack composability.
For example, let\'s say I have a v
Back in 2000, the problem was already noted. Gary Powell and Martin Weiser came up with a "view" concept, and coined the name "View Template Library". It didn't take off then but the idea makes sense. A "view" adaptor essentially applies an on-the-fly transform. For instance, it can adapt the value_type
.
The concept probably should be readdressed now we have C++0x. We've made quite some progress in generic programming since 2000.
For example, let's use the vector
to vector
example. That could be quite simple:
std::vector> values = GetValues();
vtl2::view v (values, [](std::pair p) { return p.first });
std::vector result(view.begin(), view.end());
Or, using the boost::bind
techniques, even simpler:
std::vector> values = GetValues();
vtl2::view v (values, &std::pair::first);
std::vector result(view.begin(), view.end());