This is very nice library BUT it is compiled very slowly !! Especially if you include <boost/type_traits.hpp>
Beware !
So I had to write my own implementation of is_*** since boost is slow
This is very nice library BUT it is compiled very slowly !! Especially if you include <boost/type_traits.hpp>
Beware !
So I had to write my own implementation of is_*** since boost is slow
Many people use containers with begin and end methods.
I do not love for loops for iterating over containers, it is very ugly and not maintanable.
The past solution was to use std::for_each + functors.
It can be good, but thus you need to define a global helper class for a small thing. So many used to create highly extendable class that do anything.
And of course there is std::bind1st, std::bind2nd, std::mem_fun, std::ptr_fun ugliness. Do not use it !!
And there is boost::bind the great thing, and boost::lambda the thing. I love the both. They help you to write a short code which does what it meant to be and you have a local function.
But still you cannot do all using boost::lambda , perhaps it compiles slow and produces not optimizable code.
The only solution for optimizing is to use loop.. Not now !
Boost has Boost.Foreach library which helps you to iterate in a very neat way over the container/built in array type/string literal. All what you need. But it is Boost and it is macro, and it does not provide an optimizable code even if it is better than Boost.Lambda.
The story begins with discovery of for each in VC8. VC8 has C++/CLI which must have for each to iterate over System::Array or any type derived from System::IEnumerator. But in fact for each works on Native programs too.
Even more it produces the same code like for. And it can be used with Standard Library !
Wow !
The happiness is good but it has disadvantage, it cannot get reference in the item type.
After seeking for a solution for this, and for full replacement to BOOST_FOREACH which can use Boost.Range for extandability. I have found a solution.
In Boost – Ticket #1295 there is my solution for foreach which uses advantage of VC 8 and later.
Update:
foreach for poor, it is implementation of for each which passes almost all boost.foreach tests except noncopyable, rvalue_const, rvalue_nonconst.
Enjoy
Update2:
If you lucky user of VC 9.0 or even VC 9.0 SP1you may have paid attention that BOOST_FOREACH produces exactly the same code like ugly hand-writed for loop, and i mean the for loop with end caching which many forget to do !
So there is no need for MS specific for each at all. Just use VC 9.0 SP1
C strikes C++ when it is need to work on the prepared data in the memory.
C++ has no good way of bewaring copies.
E.g. we have a prepared array:
int i[10];
and we have a function
void f(std::vector<int>& o);
We cannot pass the array into the function without copying ! Sux.
So if your library has a bad design, like this before. You stuck with copies, and you have nothing to do.
The only solution is a good design for your library, and of course C++ Standard Library has a good design to allow you not to do copies !
So what the solution ? As do you understand clearly – Iterators !
Instead of writing:
void f(std::vector<int>& o)
Write:
void f(int* first, int* last);
Yes, of course, this is not what you want, but now you do not have to copy. Heh, of course this is not a solution, it yucks with ugly pointers. And you have no meaning if you need to allocate or deallocate the memory.
So you want template ?
template<typename T> void f(T first, T last)
Not good too, it is template, so cannot be virtual function and it has 2 arguments instead of one.
So the perfect solution is range. Range it is concept of keeping two iterators for first and last.
Of course boost has iterator_range which can be used.
Something like:
void f(iterator_range<int> r)
Of course we may need implicit creation of iterator_range from vector if we do not want to change code too much.
But we still stucked if we want to use the vector methods, like index operator, or anything else. What we need is an ultimate range which can behave like vector still being not vector.
Since boost::iterator_range supports all what we need it can be some perfect solution.
Added:
Of course in each solution there is a disadvantage. As you can see in this link: http://boost.org/libs/range/doc/utility_class.html
boost::iterator_range and boost::sub_range receives an iterator rather than type, so u cannot write a function of all types of iterators unless it is template.
But you can always use the specialized version: void f(boost::iterator_range<int*> const& r).
Powered by WordPress.com