Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-10-30 13:25:02


Yurij Zhacun wrote:
> Hi.
> I start learning boost.Here is very simple code I can't compile.
>
>
> typedef std::vector<LPCWSTR> type;
> type test;
> test[0] = (_T("1"));
> test[1] = (_T("2"));
> test[2] = (_T("3"));
>
> std::for_each(test.begin(),test.end(), OutputDebugString(_1));
>
> Got compilation error :
> -----------------------------------------------------------
> error C2664: 'OutputDebugStringW' : cannot convert parameter 1 from
> 'boost::lambda:laceholder1_type' to 'LPCWSTR'
> -----------------------------------------------------------
>
> Can anybody point me at mistakes I made in the code?

Since OutputDebugStringW (which is a result of OutputDebugString macro
expansion) is a mere function, the expression OutputDebugString(_1)
means that you actually try to call it and pass its result to the for_each.

What you need is to create a functor that would delay that call. You
should try something like this:

std::for_each(test.begin(),test.end(),
  boost::lambda::bind(&OutputDebugString, boost::lambda::_1));

or

std::for_each(test.begin(),test.end(),
  boost::bind(&OutputDebugString, _1));

The first is Boost.Lambda, the second is Boost.Bind.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk