简体   繁体   中英

boost::variant and std::find_if

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.

I think that using boost bind should work :

 std::find_if (nodes.begin(), nodes.end(),
               boost::bind(&boost::apply_visitor<IsFileNodeVisitor,Node>,
                           IsFileNodeVisitor(), _1 )
              );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM