【发布时间】:2010-02-26 18:03:22
【问题描述】:
下面的 Eric 的 cmets 解决了第一个问题,但导致了我在水平规则之后描述的次要问题。谢谢埃里克!
我正在尝试将作为模板类的仿函数与两个参数一起传递给 boost thread_group 类的 create_thread 方法。但是我似乎无法超越我当前的编译错误。使用以下代码:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/thread.hpp>
#include <vector>
using namespace boost::lambda;
using namespace std;
namespace bl = boost::lambda;
template<typename ftor, typename data>
class Foo
{
public:
explicit Foo()
{
}
void doFtor ()
{
_threads.create_thread(bind(&Foo<ftor, data>::_ftor, _list.begin(), _list.end()));
//_threads.create_thread(bind(_ftor, _list.begin(), _list.end()));
_threads.join_all();
}
private:
boost::thread_group _threads;
ftor _ftor;
vector<data> _list;
};
template<typename data>
class Ftor
{
public:
//template <class Args> struct sig { typedef void type; }
explicit Ftor () {}
void operator() (typename vector<data>::iterator &startItr, typename vector<data>::iterator &endItr)
{
for_each(startItr, endItr, cout << bl::_1 << constant("."));
}
}
我还尝试了 typedef-ing 'type',因为我认为我的问题可能与 Sig 模板有关,因为函子本身是模板化的。
我得到的错误是:
error: no matching function for call to ‘boost::lambda::function_adaptor<Ftor<int> Foo<Ftor<int>, int>::*>::apply(Ftor<int> Foo<Ftor<int>, int>::* const&, const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>> >&, const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)’
事先有一堆序言。
提前感谢您的帮助!
好的,我根据 Eric 的建议修改了代码,得到了以下代码:
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/thread.hpp>
#include <vector>
using namespace boost::lambda;
using namespace std;
namespace bl = boost::lambda;
template<typename ftor, typename data>
class Foo
{
public:
explicit Foo()
{
}
void doFtor ()
{
_threads.create_thread(bl::bind(boost::ref(_ftor), _list.begin(), _list.end()));
_threads.join_all();
}
private:
boost::thread_group _threads;
ftor _ftor;
vector<data> _list;
};
template<typename data>
class Ftor
{
public:
typedef void result_type;
explicit Ftor () {}
result_type operator() (typename vector<data>::iterator &startItr, typename vector<data>::iterator &endItr)
{
for_each(startItr, endItr, cout << bl::_1 << constant("."));
return ;
}
};
但是这会导致另一个编译错误:
/usr/local/include/boost/lambda/detail/function_adaptors.hpp:45: error: no match for call to ‘(Ftor<int>) (const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&, const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)’
ftor.h:41: note: candidates are: void Ftor<data>::operator()(typename std::vector<data, std::allocator<_CharT> >::iterator&, typename std::vector<data, std::allocator<_CharT> >::iterator&) [with data = int]
/usr/local/include/boost/lambda/detail/function_adaptors.hpp:45: error: return-statement with a value, in function returning 'void'
似乎已经将 void 定义为 result_type 它现在期望 operator() 返回一些东西。我尝试从函数中返回 result_type ,但这也产生了错误。有什么想法吗?
【问题讨论】:
-
您使用的是什么编译器以及什么版本的 boost?这里编译得很好,只是缺少 iostream 标头(GCC 4.3.3,boost 1.42)
-
有趣。我在我的 Mac 上试过这个,不确定 gcc 版本,但它使用 boost 1.42。我已经在我的 linux 机器上复制了它,它使用了稍微旧的版本:gcc 4.1.1 和 boost 1.37
-
所示代码可以编译,但实例化
Foo::doFtor()会导致编译错误。例如,尝试添加int main(){Foo<Ftor<int>, int> f; f.doFtor();} -
啊,是的,我也应该提供我的主要内容。除非实例化/调用,否则编译器不会为 doFtor 方法生成代码。
-
要清楚,这是对我有用的代码:codepad.org/j4mibHWS
标签: c++ templates lambda functor