Boost.lambda: can I do this with a lambda function (multiplestatements?
Hi, I want to embed some debug printing in a lambda function which is a predicate used to remove elements from a vector like this: collection.erase( std::remove_if( collection.begin(), collection.end(), boost::lambda::if_then_else_return( boost::lambda::_1 <= "s5", // if (std::cout << "deleted: " << boost::lambda::_1, true), // then (std::cout << "not deleted: " << boost::lambda::_1, false ))), // else collection.end()); This actually compiles, runs and removes all the right elements but it does not print for every element it is checking. It just prints "not deleted: deleted: s4s5s6s8" , the last string being all the elements of the collection. Is what I want possible and if so how? TIA, -- Groeten, Joost Kraaijeveld Askesis B.V. Molukkenstraat 14 6524NB Nijmegen tel: 024-3888063 / 06-51855277 fax: 024-3608416 web: www.askesis.nl
AMDG Joost Kraaijeveld wrote:
I want to embed some debug printing in a lambda function which is a predicate used to remove elements from a vector like this:
collection.erase( std::remove_if( collection.begin(), collection.end(), boost::lambda::if_then_else_return( boost::lambda::_1 <= "s5", // if (std::cout << "deleted: " << boost::lambda::_1, true), // then (std::cout << "not deleted: " << boost::lambda::_1, false ))), // else collection.end());
This actually compiles, runs and removes all the right elements but it does not print for every element it is checking.
It just prints "not deleted: deleted: s4s5s6s8" , the last string being all the elements of the collection.
Is what I want possible and if so how?
std::cout << "deleted" is executed immediately. (This is a limitation of expression templates. It can't be made to work) Try std::cout << boost::lambda::constant("deleted") << ... similarly for not deleted. In Christ, Steven Watanabe
participants (2)
-
Joost Kraaijeveld
-
Steven Watanabe