使用 boost::bind 输出作为数组下标
如何让 boost::bind 与数组下标一起使用?这就是我想要实现的目标。请指教。
[servail: C++Progs]$ g++ -v
从 /usr/lib/gcc/i386-redhat-linux/3.4.6/specs 读取规格
配置为: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr /share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386- redhat-linux
线程模型:posix
gcc版本3.4.6 20060404(红帽3.4.6-3)
[servenail: C++Progs]$ cat t-array_bind.cpp
#include <map>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace boost::lambda;
class X
{
public:
X(int x0) : m_x(x0)
{
}
void f()
{
cout << "Inside function f(): object state = " << m_x << "\n";
}
private:
int m_x;
};
int main()
{
X x1(10);
X x2(20);
X* array[2] = {&x1, &x2};
map<int,int> m;
m.insert(make_pair(1, 0));
m.insert(make_pair(2, 1));
for_each(m.begin(),
m.end(),
array[bind(&map<int,int>::value_type::second, _1)]->f());
}
[servenail: C++Progs]$ g++ -o t-array_bind t-array_bind.cpp t-array_bind.cpp:在函数“int main()”中: t-array_bind.cpp:40: 错误: 与
中的“operator[]”不匹配 '数组[boost::lambda::bind(const Arg1&, const Arg2&) [其中 Arg1 = int std::pair::*,Arg2 = boost::lambda::lambda_functor >](((const boost::lambda::lambda_functor >&)(+boost::lambda::::_1)))]'
非常感谢。
How do I get boost::bind to work with array subscripts? Here's what I'm trying to achieve. Please advice.
[servenail: C++Progs]$ g++ -v
Reading specs from /usr/lib/gcc/i386-redhat-linux/3.4.6/specs
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr /share/info --enable-shared --enable-threads=posix --disable-checking --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-java-awt=gtk --host=i386- redhat-linux
Thread model: posix
gcc version 3.4.6 20060404 (Red Hat 3.4.6-3)
[servenail: C++Progs]$ cat t-array_bind.cpp
#include <map>
#include <string>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <iostream>
using namespace std;
using namespace boost;
using namespace boost::lambda;
class X
{
public:
X(int x0) : m_x(x0)
{
}
void f()
{
cout << "Inside function f(): object state = " << m_x << "\n";
}
private:
int m_x;
};
int main()
{
X x1(10);
X x2(20);
X* array[2] = {&x1, &x2};
map<int,int> m;
m.insert(make_pair(1, 0));
m.insert(make_pair(2, 1));
for_each(m.begin(),
m.end(),
array[bind(&map<int,int>::value_type::second, _1)]->f());
}
[servenail: C++Progs]$ g++ -o t-array_bind t-array_bind.cpp
t-array_bind.cpp: In function `int main()':
t-array_bind.cpp:40: error: no match for 'operator[]' in
'array[boost::lambda::bind(const
Arg1&, const Arg2&) [with Arg1 = int
std::pair::*, Arg2 =
boost::lambda::lambda_functor >](((const boost::lambda::lambda_functor >&)(+boost::lambda::::_1)))]'
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Charles 所解释的,boost::bind 返回一个函数对象而不是一个整数。将为每个成员评估函数对象。一个小帮助器结构将解决这个问题:
编辑:我已经更改了函子以返回数组的第 n 个元素。
As Charles has explained, boost::bind returns a function object and not an integer. The function object will be evaluated for each member. A little helper struct will solve the problem:
Edit: I've changed the functor to return the nth element of the array.
您的代码无法编译的原因是
boost::bind
的返回值不是可以用作数组下标的整数类型。相反,boost::bind
返回一个未指定类型的实现定义函数对象,可以使用()
运算符调用。The reason your code doesn't compile is because the return value of
boost::bind
is not an integer type that can be used as an array subscript. Rather,boost::bind
returns an implementation-defined function object of an unspecified type, which can be called using the()
operator.