简体   繁体   English

C ++函子绑定

[英]C++ functor binding

i tried to use the old bind2nd function in this way: 我试图以这种方式使用旧的bind2nd函数:

template<typename T>
class printer
{
public:
  void operator()(T a, string& kd)
  {
        cout<<a<<endl;
  }
};


int main(int argc, char *argv[])
{
   string nme = "J-dar";
   auto f1 = bind2nd(printer<int>(),nme);

   //f1(5);
   return 0;
}

but i get a lot of errors: 但是我有很多错误:

required from here
error: no type named 'first_argument_type' in 'class printer<int>'  class binder2nd        ^
error: no type named 'second_argument_type' in 'class printer<int>'    typename _Operation::second_argument_type value;                                              ^
error: no type named 'second_argument_type' in 'class printer<int>'    binder2nd(const _Operation& __x,    ^
error: no type named 'result_type' in 'class printer<int>'    operator()(const typename _Operation::first_argument_type& __x) const    ^
error: no type named 'result_type' in 'class printer<int>'    operator()(typename      _Operation::first_argument_type& __x) const    ^
required from here
error: no type named 'second_argument_type' in 'class printer<int>'    typedef typename _Operation::second_argument_type _Arg2_type;                                           

from what i can see it's all correct so i don't really know what is going on. 从我所看到的一切都是正确的,所以我真的不知道发生了什么。 ^ ^

First of all : I would recommend using abandoning bind1st() and bind2nd() , which are deprecated in C+11, and in general the obsolete support for functional programming of the C++03 Standard Library. 首先 :我建议使用C + 11中已弃用的bind1st()bind2nd() ,并且通常会过时地支持C ++ 03 Standard Library的功能编程。

You should rather use C++11's std::bind() , since it seems you can afford that - judging from the fact that you are using the auto keyword: 您应该使用C ++ 11的std::bind() ,因为看来您可以负担得起-从您使用auto关键字的事实来看:

#include <functional>

// ...

auto f1 = std::bind(printer<int>(), std::placeholders::_1, nme);

This said, just for the record, the deprecated std::bind2nd() function requires some metadata about the signature of your functor's call operator, and it expects these metadata to be provided as type aliases in your functor class. 这就是说,仅作记录用途,不推荐使用的std::bind2nd()函数需要一些有关函子的调用运算符签名的元数据,并且希望这些元数据作为函子类中的类型别名提供。 For instance: 例如:

template<typename T>
class printer
{
public:

    // These metadata must be present in order for bind1st and bind2nd to work...
    typedef void result_type;
    typedef T first_argument_type;
    typedef string const& second_argument_type;

    void operator()(T a, string const& kd) const
//                                         ^^^^^ // Bonus advice #1:
//                                               // This could and should be
//                                               // const-qualified
//                              ^^^^^
//                              Bonus advice #2: why not taking by
//                              reference to const here? ;)
    {
        cout<<a<<endl;
    }
};

A simpler way of achieving the above is to use the (also deprecated) class template std::binary_function as a base class, and let that class template define the appropriate type aliases: 实现上述目标的一种更简单的方法是使用(也已弃用)类模板std::binary_function作为基类,并让该类模板定义适当的类型别名:

template<typename T>
class printer : public std::binary_function<T, string const&, void>
//              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
{
public:
    void operator()(T a, string const& kd) const
    {
        cout<<a<<endl;
    }
};

But again, please consider putting std::bind1st() , std::bind2nd() , as well as std::unary_function and std::binary_function , back in the drawer. 但是同样,请考虑将std::bind1st()std::bind2nd()以及std::unary_functionstd::binary_function放回抽屉中。 They are superseded by C++11's more powerful support for functional programming. 它们被C ++ 11对函数式编程的更强大支持所取代。

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

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