On Mar 8, 2006, at 3:21 PM, 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?
I can spot one thing at least: Arguments to if_then_else must be lambda functions:
if_then_else(bind(&fs::is_directory, _1), var(cout) << "D\n", var(cout) << "~D\n"));
This delays the evaluation of the branches (which would otherwise just print D or ~D once, when the lambda function is created, rather than during every iteration of for_each Best, Jaakko