Question

How do I use a visitor with the find_if function? I'm guessing I need to do some class of magical bind and therefore this will not work:

    typedef boost::variant<FileNode,DirectoryNode> Node;
    typedef std::vector<Node> Nodes;
    const Nodes& nodes;
    IsFileNodeVisitor isFileNodeVisitor;
    find_if (nodes.begin(), nodes.end(), isFileNodeVisitor);

    class IsFileNodeVisitor: public boost::static_visitor<bool>
    {
    public:
        bool operator()(const FileNode&) const {
            return true;
        }

        bool operator()(const DirectoryNode&) const {
            return false;
        }
    };

The idea of the code above is to give me an iterator to the first FileNode instance in the vector of nodes.

Was it helpful?

Solution

I think that using boost bind should work :

 std::find_if (nodes.begin(), nodes.end(),
               boost::bind(&boost::apply_visitor<IsFileNodeVisitor,Node>,
                           IsFileNodeVisitor(), _1 )
              );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top