我尝试将 boost::bind 与 boost::factory 一起使用,但没有成功,
我有此类 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: 错误: 'a[boost::_bi::storage3; 中的 'operator[]' 不匹配>::a3_ [其中 A1 = boost::_bi::value, std::allocator; > >,A2 = boost::_bi::value,std::allocator; > >, int I = 1]]'
我做错了什么?
I'm trying without success to use a boost::bind with a boost::factory
I have this class Zambas with 4 arguments (2 strings and 2 ints) and
class Zambas {
public:
Zambas(const std::string&, const std::string&,int z1=0,int z2=0) {
if (z1==z2){
}
}
};
inside other method i have the following call
boost::function<Zambas*()> f(boost::bind(boost::factory<Zambas*>(), std::string(""), std::string(""),_1,_2));
that fails with the following compiler error:
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]]’
What I'm doing wrong ?
发布评论
评论(1)
bind
函数返回一个双参数函子,因为您将构造函数的第三个和第四个参数绑定到占位符值_1
和_2
。但是,您将结果存储在零参数function
对象中。我发现六年前的参考资料解释说你可以'绑定函数时不要省略参数,即使它们是使用默认值声明的。
我认为您有三个选择:
bind
时提供实际的int
值,而不是占位符。f
的声明以指示它存储双参数函数,然后在调用它时始终提供两个值。f
之前为这些变量赋值。最后一个选项可能只会使您的代码更难阅读,而没有太多好处,因此更喜欢前两个选项之一。
The
bind
function returns a two-argument functor because you bound the third and fourth parameters of your constructor to the placeholder values_1
and_2
. However, you're storing the result in a zero-argumentfunction
object.I found a reference from six years ago explaining that you can't omit parameters when you bind a function, even if they're declared with default values.
I think you have three options:
int
values in your call tobind
instead of placeholders.f
to indicate that it stores a two-argument function, and then always provide both values when you call it.f
.The last option will probably just make your code harder to read without much benefit, so prefer one of the first two options instead.