Scott Meyers wrote:
Consider this (working) code using Boost.FileSystem:
fs::directory_iterator di(dir); fs::directory_iterator end; while (di != end) { if (is_directory(*di)) cout << "D\n"; else cout << "~D\n"; ++di; }
This looks like it should be lambda-able. Here's my attempt:
for_each(fs::directory_iterator(dir), fs::directory_iterator(), if_then_else(bind(&fs::is_directory, _1), cout << "D\n", cout << "~D\n"));
VC7.1 doesn't like this -- the diagnostics are below. Any idea how I can make this work?
The cout << "D\n" subexpression is evaluated immediately since it doesn't have any lambda components. Use var(cout) << "D\n" instead, or the ?: variant: cout << if_then_else_return( bind(&fs::is_directory, _1), "D\n", "~D\n" ) You could also make an alias for var(std::cout): var_typestd::ostream::type cout = var( std::cout ); and use that in the lambda expressions.