boost::lambda question

OK, I'm a bit of a boost noob. I have a container of std::wstring's and I want to use a lambda expression in a for_each loop to pass each string to a WinAPI function - however, I need access to the c_str() method, and this is giving me a problem. For example, if I want to use the DeleteFile WinAPI call, I want to write something like: std::vectorstd::wstring v; ... for_each(v.begin(), v.end(), ::DeleteFile(_1)); But the above won't work as I need to pass the 'C' string to ::DeleteFile. So, I really want to be able to do something like this: for_each(v.begin(), v.end(), ::DeleteFile(_1.c_str())); Any ideas how I can access a member function (like c_str()) using a lambda placeholder?

Robert Caldecott wrote:
OK, I'm a bit of a boost noob. I have a container of std::wstring's and I want to use a lambda expression in a for_each loop to pass each string to a WinAPI function - however, I need access to the c_str() method, and this is giving me a problem.
For example, if I want to use the DeleteFile WinAPI call, I want to write something like:
std::vectorstd::wstring v; ... for_each(v.begin(), v.end(), ::DeleteFile(_1));
But the above won't work as I need to pass the 'C' string to ::DeleteFile.
So, I really want to be able to do something like this:
for_each(v.begin(), v.end(), ::DeleteFile(_1.c_str()));
Any ideas how I can access a member function (like c_str()) using a lambda placeholder?
You need to use the binders of lambda, like this: for_each(v.begin(), v.end(), ::DeleteFile(bind(&std::wstring::c_str, _1))); Sebastian Redl

Thanks Sebastian, but I am still struggling to make this work. I now get
the following error:
"error C2664: 'DeleteFileW' : cannot convert parameter 1 from 'const
boost::lambda::lambda_functor<T>' to 'LPCWSTR'"
A small sample:
#include
Robert Caldecott wrote:
OK, I'm a bit of a boost noob. I have a container of std::wstring's and I want to use a lambda expression in a for_each loop to pass each string to a WinAPI function - however, I need access to the c_str() method, and this is giving me a problem.
For example, if I want to use the DeleteFile WinAPI call, I want to write something like:
std::vectorstd::wstring v; ... for_each(v.begin(), v.end(), ::DeleteFile(_1));
But the above won't work as I need to pass the 'C' string to ::DeleteFile.
So, I really want to be able to do something like this:
for_each(v.begin(), v.end(), ::DeleteFile(_1.c_str()));
Any ideas how I can access a member function (like c_str()) using a lambda placeholder?
You need to use the binders of lambda, like this:
for_each(v.begin(), v.end(), ::DeleteFile(bind(&std::wstring::c_str, _1)));
Sebastian Redl

OK, thanks to Sebastian and Nat, plus some Google group searches, I found a
solution, which is, in a nutshell - #define BOOST_BIND_ENABLE_STDCALL:
Here is a sample:
#include
For example, if I want to use the DeleteFile WinAPI call, I want to write something like:
std::vectorstd::wstring v; ... for_each(v.begin(), v.end(), ::DeleteFile(_1));
But the above won't work as I need to pass the 'C' string to ::DeleteFile.
So, I really want to be able to do something like this:
for_each(v.begin(), v.end(), ::DeleteFile(_1.c_str()));
participants (3)
-
Rob Caldecott
-
Robert Caldecott
-
Sebastian Redl