filesystem lambda and for_each

hello, Despite some research, I am struggling to combine boost filesystem, boost lambda and STL for_each. 32 std::string str = ""; 33 std::string path = "/tmp"; 34 boost::filesystem::directory_iterator it(path); 35 while(it != boost::filesystem::directory_iterator()) 36 { 37 str += it->path().filename() + "<br>"; 38 ++it; 39 } works fine but 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’ 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: thanks!

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
participants (2)
-
Jean-Christophe Roux
-
Nathan Crookston