简体   繁体   English

lambda表达式中的C ++模板函子

[英]C++ templated functor in lambda expression

This first piece has been solved by Eric's comments below but has led onto a secondary issue that I describe after the horizontal rule. 第一篇文章已经通过Eric的评论在下面得到了解决,但却引发了我在横向规则之后描述的第二个问题。 Thanks Eric! 谢谢埃里克!

I'm trying to pass a functor that is a templated class to the create_thread method of boost thread_group class along with two parameters to the functor. 我试图将一个模板类的仿函数传递给boost thread_group类的create_thread方法以及两个参数到仿函数。 However I can't seem to get beyond my current compile error. 但是我似乎无法超越我当前的编译错误。 With the below code: 使用以下代码:

#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("."));
    }
}

I also tried typedef-ing 'type' as I thought my problem might have something to do with the Sig Template as the functor itself is templated. 我也尝试过typedefing'type',因为我认为我的问题可能与Sig模板有关,因为函子本身是模板化的。

The error I am getting is: 我得到的错误是:

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> > >&)’

with a bunch of preamble beforehand. 事先用一堆序言。

Thanks in advance for any help! 在此先感谢您的帮助!


Okay I've modified the code taking in Eric's suggestions below resulting in the following code: 好的,我已经修改了以下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 ;
    }
};

However this results in another compile error: 但是,这会导致另一个编译错误:

/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'

It seems having defined void as a result_type it is now expecting the operator() to return something. 似乎已将void定义为result_type,现在它期望operator()返回一些东西。 I tried returning result_type from within the function but this also generated errors. 我尝试从函数中返回result_type,但这也产生了错误。 Any ideas? 有任何想法吗?

Sig (or in your case, simply typedef void result_type; is necessary. Sig (或者在你的情况下,只需要typedef void result_type;是必要的。

IIRC, lambda::bind makes const copies of its arguments. IIRC,lambda :: bind生成其参数的const副本。

There is thus a problem with functors with non-const operator(). 因此,使用非const运算符()的仿函数存在问题。 This is solved by making Ftor::operator()const or by wrapping (in doFtor()), _ftor with boost::ref 这是通过制作Ftor :: operator()const 通过包装(在doFtor()中),_ftor和boost :: ref来解决的

There is a similar problem with the iterators. 迭代器也存在类似的问题。 Wrapping in boost::ref here won't work directly because it would end up using a reference to a temporary. 在这里包装boost :: ref将无法直接工作,因为它最终会使用对临时的引用。 The simpler solution is to modify Ftor::operator() to take its arguments by copy. 更简单的解决方案是修改Ftor :: operator()以通过副本获取其参数。

The simplest is thus to modify Ftor so that its operator() is const and it takes its arguments by copy: 因此,最简单的是修改Ftor,使其operator()为const, 通过副本获取其参数:

void operator() (typename vector<data>::iterator startItr, typename vector<data>::iterator endItr)const

If you really can't make Ftor::operator() const, you could modify doFtor() as follows (but it is still necessary to make Ftor::operator() take its arguments by copy): 如果你真的不能创建Ftor :: operator()const,你可以修改doFtor()如下(但仍然需要使Ftor :: operator()通过副本获取其参数):

_threads.create_thread(bind(boost::ref(_ftor), _list.begin(), _list.end()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM