Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-09-15 18:40:00


Alexander Shyrokov wrote:
>> In such a situation I tend to define my own nullptr:
>>
>> struct nullptr_t
>> {
>> template<class T> operator T* () const { return 0; }
>> } nullptr;
>>
>> and use that instead of NULL.
> Thanks, Peter. Both casting and nullptr worked fine. But it brings me
> to a bigger question, should I bother replacing this code:
>
> struct c{void f(char*){}};
> typedef std::map<int,c*> TMap;
> TMap a;
> for(TMap::iterator i=a.begin();i!=a.end();++i)(*a)->second.f(NULL):
>
> with the more complex code like this:
>
> struct c{void f(char*){}};
> struct nullptr_t{template<class T>operator T*()const{return
> 0;}}nullptr; typedef std::map<int,c*> TMap;
> TMap a;
> std::for_each(a.begin(),a.end(),
> boost::bind(&c::f,
> boost::bind(&TMap::value_type::second,_1),
> nullptr)
> );
>
> I know there should be a line. But I am new to the algorithm's way :)

Good question. I tend to prefer

for_each_pair( a.begin(), a.end(), boost::bind( &c::f, _2, nullptr ) );

over the explicit loop, and two nested binds aren't automatically out of the
question, but for anything more than that the low-tech approach wins. But
this is subjective, and it also depends on who'll be reading the code.

I don't include for_each_pair and nullptr as part of the complexity since
they are one-time investments that can be reused.


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