我尝试将boost::bind
与std::vector<>::resize
一起使用。
但以下代码无法编译:
#include <boost/bind.hpp>
#include <vector>
using namespace boost;
int main(){
typedef std::vector<double> type;
type a;
bind(&type::resize, _1, 2)(a);
return 0;
}
那么,我该怎么做呢?
谢谢!
提升版本1.53 gcc版本4.8或4.6
* 编辑:* 以上代码适用于-std = c ++ 11。事实上,我原来的问题是:
#include <boost/bind.hpp>
#include <blitz/array.h>
#include <vector>
using namespace boost;
using namespace blitz;
int main(){
typedef Array<double, 1> type;
type a;
//a.resize(4) is ok;
bind(&type::resize, _1, 2)(a);
return 0;
}
我的编译命令是: g ++ t.cpp -I path / include / -std = c ++ 11 -L path / lib / -l blitz
答案 0 :(得分:7)
resize
可能是一个重载函数(在C ++ 11中它必须是),所以你需要告诉编译器你需要哪个重载。对于一个参数形式,这应该适用于C ++ 11:
bind(static_cast<void (type::*)(type::size_type)>(&type::resize), _1, 2)(a);
或更可读:
typedef void (type::*resize_signature)(type::size_type);
bind(static_cast<resize_signature>(&type::resize), _1, 2)(a);
如果不是一个重载函数(与C ++ 03模式下的GCC一样),那么它需要两个参数(一个具有默认值),你需要提供第二个参数,因为{{ 1}}不能使用默认参数:
bind
不幸的是,这个C ++ 03版本不可移植,允许实现使用单个函数或一对重载。要使其可移植,或与typedef void (type::*resize_signature)(type::size_type, const value_type&);
bind(static_cast<resize_signature>(&type::resize), _1, 2, 0.0)(a);
等其他类型一起使用,您可以将调用包装在调用Array
的自定义仿函数中,这样您就不需要知道确切的签名:
resize
或者在C ++ 11中只使用lambda表达式而不是typename<class VecT>
struct resizer<VecT> {
void operator()(VecT& v, unsigned n) const { v.resize(n); }
};
// ...
bind(resizer<type>(), _1, 2)(a);
:
bind