[英]Problem with boost::bind, boost::function and boost::factory
我正在嘗試使用boost :: factory的boost :: bind而沒有成功
我有這個類Zambas有4個參數(2個字符串和2個整數)和
class Zambas {
public:
Zambas(const std::string&, const std::string&,int z1=0,int z2=0) {
if (z1==z2){
}
}
};
在其他方法中我有以下調用
boost::function<Zambas*()> f(boost::bind(boost::factory<Zambas*>(), std::string(""), std::string(""),_1,_2));
失敗並出現以下編譯器錯誤:
bind.hpp:382: error: no match for 'operator[]' in 'a[boost::_bi::storage3<A1, A2, boost::arg<I> >::a3_ [with A1 = boost::_bi::value<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, A2 = boost::_bi::value<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, int I = 1]]'
我做錯了什么?
bind
函數返回一個雙參數仿函數,因為您將構造函數的第三個和第四個參數綁定到占位符值_1
和_2
。 但是,您將結果存儲在零參數function
對象中。
我發現六年前的一個引用解釋了綁定函數時不能省略參數, 即使它們是用默認值聲明的 。
我認為你有三個選擇:
int
在您的通話值bind
,而不是占位符。 f
的聲明以指示它存儲雙參數函數,然后在調用它時始終提供這兩個值。 f
之前為這些變量賦值。 最后一個選項可能只是讓你的代碼更難以閱讀而沒有太大的好處,所以更喜歡前兩個選項中的一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.