C ++ boost :: bind / lambda and operator bool ()

How do I bind a conversion operator to bool using boost :: bind or boost :: lambda?

For example, suppose I have a class C, with the operator bool () and a list<C>. How to use remove_if and bind / lambda to remove all elements that are converted to false when converted to bool?

+3
source share
2 answers

You do not need to use std::bindor for this std::remove_if; std::removewill be sufficient:

std::vector<T> v; // Assuming T provides some conversion to bool

// Remove all elements that evaluate to 'false':
v.erase(std::remove(v.begin(), v.end(), false), v.end());

Or you can use a function object std::logical_notwith std::remove_if:

v.erase(std::remove_if(v.begin(), v.end(), std::logical_not<T>()), v.end());

, operator bool(): - ++, , , , , . safe-bool operator bool(). , operator bool(), safe-bool .

+5

std:: logical_not, , false; if true, :

remove_if(..., ..., bind(&C::operator bool, _1));
0

Source: https://habr.com/ru/post/1790464/


All Articles