Boost logo

Boost Users :

Subject: Re: [Boost-users] filesystem lambda and for_each
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2010-08-19 15:48:48


Hi Jean-Christophe,

>  40   std::for_each(boost::filesystem::directory_iterator(path),
> boost::filesystem::directory_iterator(),
>  41                 str += *boost::lambda::_1.path().parent_path() );
> does not compile with the error message that :
>  error: ‘const struct
> boost::lambda::lambda_functor<boost::lambda::placeholder<1> >’ has no member
> named ‘path’

Unfortunately, C++ doesn't allow you to overload the '.' operator, so
we have to settle for bind calls. You might try the following:

#include <boost/filesystem.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/lambda/lambda.hpp>

#include <iostream>

namespace bf = boost::filesystem;
namespace ll = boost::lambda;

//tested w/ cygwin g++3, boost 1.44
int main(int argc, char* argv[])
{
  std::string str = "";
  std::string path = ".";
  std::for_each(bf::directory_iterator(path), bf::directory_iterator(),
      str += ll::bind(&bf::directory_entry::filename, ll::_1));

  std::cout << str << std::endl;

  return 0;
}

> I don't understand why and I don't really see how I could have this work.
> Also, I am wondering why with
> _1
> instead of
> boost::lambda::_1
>  I am getting:
> error: reference to ‘_1’ is ambiguous
> /usr/local/include/boost/lambda/core.hpp:69:

boost::bind declares _1 in an unnamed namespace, which means that
unqualified calls to _1 will usually pick it up (assuming boost::bind
has been #included somewhere. I seem to recall that std::bind put its
placeholders in std::placeholders instead, I don't know if boost::bind
will put them somewhere similar or not. . .

HTH,

Nate


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