2010-12-09 54 views
1

我有一個提升的定義如下::函數對象:的boost ::功能和多參數成員函數

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

我通過這個功能結構構造函數的參數:

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; 
}; 

現在我需要通過struct add_node_value_visitor以下函數,但boost::function<T>不接受2 arg成員函數,在它說我應該使用boost :: bind的文檔,但我不知道我該怎麼做,看到我也必須滿足我的boost :: apply_visitor樂趣ction。

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); 

任何想法任何人?

回答

1

您需要提供了分解劑對象的實例,就像這樣:

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

3

很難被沒有看到ConcatValue的聲明精確,但你想要的東西,如:

boost::bind(&Decomposer::ConcatValues, some_decomposer_instance, _1, _2) 
1

答案取決於ConcatValues究竟是什麼。我猜測它實際上是一個Decomposer類的非靜態成員函數,因此它實際上並不期望兩個參數:三個參數:兩個字符串和調用它的Decomposer實例(this) 。

Boost.Bind確實是一個解決方案,因爲它會幫助你成員函數的第一個參數綁定Decomposer實例,並轉發兩個std::string通過:

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