• <i id='XN2Qu'><tr id='XN2Qu'><dt id='XN2Qu'><q id='XN2Qu'><span id='XN2Qu'><b id='XN2Qu'><form id='XN2Qu'><ins id='XN2Qu'></ins><ul id='XN2Qu'></ul><sub id='XN2Qu'></sub></form><legend id='XN2Qu'></legend><bdo id='XN2Qu'><pre id='XN2Qu'><center id='XN2Qu'></center></pre></bdo></b><th id='XN2Qu'></th></span></q></dt></tr></i><div id='XN2Qu'><tfoot id='XN2Qu'></tfoot><dl id='XN2Qu'><fieldset id='XN2Qu'></fieldset></dl></div>
    • <bdo id='XN2Qu'></bdo><ul id='XN2Qu'></ul>

      1. <tfoot id='XN2Qu'></tfoot>

        <legend id='XN2Qu'><style id='XN2Qu'><dir id='XN2Qu'><q id='XN2Qu'></q></dir></style></legend>

        <small id='XN2Qu'></small><noframes id='XN2Qu'>

        boost::bind 的返回类型是什么?

        时间:2023-08-29

          <small id='DtHG5'></small><noframes id='DtHG5'>

          • <tfoot id='DtHG5'></tfoot>

              <legend id='DtHG5'><style id='DtHG5'><dir id='DtHG5'><q id='DtHG5'></q></dir></style></legend>
                <tbody id='DtHG5'></tbody>
              <i id='DtHG5'><tr id='DtHG5'><dt id='DtHG5'><q id='DtHG5'><span id='DtHG5'><b id='DtHG5'><form id='DtHG5'><ins id='DtHG5'></ins><ul id='DtHG5'></ul><sub id='DtHG5'></sub></form><legend id='DtHG5'></legend><bdo id='DtHG5'><pre id='DtHG5'><center id='DtHG5'></center></pre></bdo></b><th id='DtHG5'></th></span></q></dt></tr></i><div id='DtHG5'><tfoot id='DtHG5'></tfoot><dl id='DtHG5'><fieldset id='DtHG5'></fieldset></dl></div>
                <bdo id='DtHG5'></bdo><ul id='DtHG5'></ul>
                  本文介绍了boost::bind 的返回类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想将一个函数的绑定器"保存到一个变量中,以便通过利用其运算符重载设施在以下代码中重复使用它.这是实际执行我想要的代码:

                  I want to save the "binder" of a function to a variable, to use it repetitively in the following code by exploiting its operator overloading facilities. Here is the code that actually does what I want:

                  #include <boost/bind.hpp>
                  #include <vector>
                  #include <algorithm>
                  #include <iostream>
                  
                  class X 
                  {       
                      int n; 
                  public: 
                      X(int i):n(i){}
                      int GetN(){return n;}  
                  };
                  
                  int main()
                  {
                      using namespace std;
                      using namespace boost;
                  
                      X arr[] = {X(13),X(-13),X(42),X(13),X(-42)};
                      vector<X> vec(arr,arr+sizeof(arr)/sizeof(X));
                  
                      _bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);
                  
                      cout << "With  n =13 : " 
                           << count_if(vec.begin(),vec.end(),bindGetN == 13)
                           << "
                  With |n|=13 : " 
                           << count_if(vec.begin(),vec.end(),bindGetN == 13 || bindGetN == -13)
                           << "
                  With |n|=42 : " 
                           << count_if(vec.begin(),vec.end(),bindGetN == 42 || bindGetN == -42) 
                           << "
                  ";
                  
                      return 0;                                                                    
                  } 
                  

                  让我烦恼的是,当然,这条线:

                  What bothers me is, of course, the line:

                  bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);
                  

                  我只是通过故意制造类型错误并分析错误消息来获得类型.这当然不是一个好方法.有没有办法获得bindGetN"的类型?或者,也许有不同的方法来产生类似的功能?

                  I've obtained the type just by deliberately making a type error and analysing the error message. That is certainly not a good way to go. Is there a way to obtain the type for the "bindGetN"? Or, maybe there are different ways to produce similar functionality?

                  我忘了提到使用 function 的标准"建议在这种情况下不起作用——因为我想让我的运算符重载.

                  I forgot to mention that the, so to say, "standard" suggestion to use function is not working in this case -- because I'd like to have my operator overloading.

                  推荐答案

                  简短的回答是:您不需要知道(实现定义).它是一个绑定表达式(std::tr1::is_bind_expression::value 对实际类型产生 true).

                  The short answer is: you don't need to know (implementation defined). It is a bind expression (std::tr1::is_bind_expression<T>::value yields true for the actual type).

                  看看

                  1. std::tr1::function<>
                  2. BOOST_AUTO()
                  3. c++0x 'auto' 关键字(类型推断)
                    • 它的近亲 decltype() 可以帮助你走得更远
                  1. std::tr1::function<>
                  2. BOOST_AUTO()
                  3. c++0x 'auto' keywords (Type Inference)
                    • it's close cousing decltype() can help you move further

                  1.

                  std::tr1::function<int> f; // can be assigned from a function pointer, a bind_expression, a function object etc
                  
                  int realfunc();
                  int realfunc2(int a);
                  
                  f = &realfunc;
                  int dummy;
                  f = tr1::bind(&realfunc2, dummy);
                  

                  2.

                  BOOST_AUTO() 旨在支持没有编译器 c++0x 支持的 c++0x auto 的语义:

                  2.

                  BOOST_AUTO() aims to support the semantics of c++0x auto without compiler c++0x support:

                  BOOST_AUTO(f,boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
                  

                  3.

                  本质上相同,但有编译器支持:

                  3.

                  Essentially the same but with compiler support:

                  template <class T> struct DoWork { /* ... */ };
                  
                  auto f = boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
                  
                  DoWork<decltype(T)> work_on_it(f); // of course, using a factory would be _fine_
                  

                  请注意,auto 可能是为这种情况而发明的:实际类型是您不想知道"并且可能因编译器/平台/库而异

                  Note that auto has probably been invented for this kind of situation: the actual type is a 'you don't want to know' and may vary across compilers/platforms/libraries

                  这篇关于boost::bind 的返回类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C++ 128/256 位固定大小整数类型 下一篇:C++ 单精度和双精度混合计算的控制规则是什么?

                  相关文章

                    <legend id='gYCw2'><style id='gYCw2'><dir id='gYCw2'><q id='gYCw2'></q></dir></style></legend>
                    1. <small id='gYCw2'></small><noframes id='gYCw2'>

                      <tfoot id='gYCw2'></tfoot>

                      <i id='gYCw2'><tr id='gYCw2'><dt id='gYCw2'><q id='gYCw2'><span id='gYCw2'><b id='gYCw2'><form id='gYCw2'><ins id='gYCw2'></ins><ul id='gYCw2'></ul><sub id='gYCw2'></sub></form><legend id='gYCw2'></legend><bdo id='gYCw2'><pre id='gYCw2'><center id='gYCw2'></center></pre></bdo></b><th id='gYCw2'></th></span></q></dt></tr></i><div id='gYCw2'><tfoot id='gYCw2'></tfoot><dl id='gYCw2'><fieldset id='gYCw2'></fieldset></dl></div>
                      • <bdo id='gYCw2'></bdo><ul id='gYCw2'></ul>