c++ - On reference_wrapper and callable objects -


itemprop = "text">

Looking at the following redeemed object:

  struct repayable: public std :: unary_function & LT; Zero, zero> {Zero operator () () const {std :: cout & lt; & Lt; "Hello World" & lt; & Lt; Std :: endl; }}; It says through  

std :: tr1 :: reference_wrapper>> :

  refundable obj; Std :: tr1 :: ref (obj) (); Instead, when  operator ()  accepts an argument: 

  struct repayable: public std :: unary_function & LT; Integer, null & gt; {Zero operator () (int n) cons {std :: cout & lt; & Lt; N & lt; & Lt; Std :: endl; }};  

std :: tr1 :: dam accepts a reference_wrapper to do this as a repayable cover ...

  repurse obj; Std :: tr1 :: bind (std :: tr1 :: ref (obj), 42) ();  

But what's wrong with this?

  std :: tr1 :: ref (obje) (42);  

G ++ - 4.4 fails to compile with the following error:

  test.cpp: 17: error: 'for call (STD no combat :: tr1 :: reference_wrapper & lt; constant repatriation & gt;) (int) '/usr/include/c++/4.4/tr1_impl/functional:462: Attention: Candidates are: typename std: : Tr1 :: result_of & lt; Typename std :: tr1 :: _ Function_to_function_pointer & lt; _Tp, std :: tr1 :: is_function :: value & gt; :: type (_Args ...) & gt; :: type std :: tr1 :: reference_use & lt; _Tp & gt; :: Operator () (_ Args & amp; ...) Const [with _Args = int, _Tp = const callable]  

The implementation of tr1 context_used G ++ - 4.4 is equipped with the following operator:

  template & lt; Typename ... _Args & gt; Typename result_of & lt; _M_func_type (_Args ...) & gt; :: Type operator () (_ Args & ... __args) const {returns __invoke (get), __args ...); }  

Takes argument by reference so the R-value logic can not be passed to the reference-ripper:

  std :: tr1 :: ref (Obj) (42);  

Instead of:

  int arg = 42; Std :: tr1 :: ref (obj) (arg);  

Just works fine.

  std :: tr1 :: bind (std :: tr1 :: ref (obj), 42) ()  

works because the bind copy Takes argument by


Comments