lambda 表达式中的 C++ 模板化函子
题
第一篇文章已通过下面埃里克的评论解决,但导致了我在水平规则之后描述的第二个问题。谢谢埃里克!
我试图将一个作为模板类的函子传递给 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> > >&)’
事先有一堆序言。
预先感谢您的任何帮助!
好的,我已经根据下面埃里克的建议修改了代码,得到了以下代码:
#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,但这也产生了错误。有任何想法吗?
解决方案
Sig
(或者在你的情况下,简单地 typedef void result_type;
是必要的。
IIRC,lambda::bind 为其参数创建常量副本。
因此,具有非常量运算符 () 的函子存在问题。通过将 Ftor::operator() 设置为 const 可以解决此问题 或者 通过用 boost::ref 包装(在 doFtor() 中), _ftor
迭代器也存在类似的问题。在这里用 boost::ref 包装不会直接起作用,因为它最终会使用对临时对象的引用。更简单的解决方案是修改 Ftor::operator() 以通过复制获取其参数。
因此,最简单的方法是修改 Ftor,使其 operator() 为 const 和 它通过复制获取参数:
void operator() (typename vector<data>::iterator startItr, typename vector<data>::iterator endItr)const
如果您确实无法使 Ftor::operator() 成为常量,则可以按如下方式修改 doFtor() (但仍然需要使 Ftor::operator() 通过复制获取其参数):
_threads.create_thread(bind(boost::ref(_ftor), _list.begin(), _list.end()));