Search code examples
c++boost-function

boost::function and multiple argument member-function


I have the following definition of a boost::function object:

typedef boost::function<std::string (std::string, std::string)> concat;

I am passing this function as a struct constructor argument:

struct add_node_value_visitor : boost::static_visitor<>
{
    typedef boost::function<std::string (std::string, std::string)> concat;
    add_node_value_visitor(concat _func, std::string key) : _func_concat(_func), _key(key) {}

    template <typename T>
    void operator() ( const T& value) const
    {
        std::string result = _func_concat(boost::lexical_cast<std::string, T>(value), _key);
    }

    std::string _key;
    concat _func_concat;
};

Now I need to pass the struct add_node_value_visitor to the following function, however the boost::function<T> does not accept the 2 arg member function, in the documentation it says I should use boost::bind, but I am however not sure how I'd do that, seeing I also have to satisfy my boost::apply_visitor function.

boost::apply_visitor( add_node_value_visitor( &Decomposer::ConcatValues, key), var); // ConcatValues takes 2 args, var = boost::variant


std::string ConcatValues(std::string str, std::string key);

Any ideas anybody?


Solution

  • You need to supply an instance of a Decomposer object, like this:

    boost::apply_visitor( add_node_value_visitor( boost::bind( &Decomposer::ConcatValues, yourDecomposer, _1, _2 ), key), var);