Friday 15 May 2015

c++ - Why the sequence-operation algorithms predicates are passed by copy? -


I am wondering why the factories are passed by the factators:

  Template & lt; Typnam T & G; Composition related (composite): result (T ()) {} zero operator () (constant tee object) {result = = value; Std :: cout & lt; & Lt; Price & lt; & Lt; ";";}; T result; }; Std :: array & lt; Int, 10 & gt; A {{1, 1, 2, 3, 5, 8, 13, 21, 34, 55}}; Summatory & lt; Integer & gt; Zodiac; Std :: cout & lt; & Lt; "Summary of \ N:"; Std :: for_each (a.begin (), a.end (), yoga); Std :: cout & lt; & Lt; "Is:" & lt; & Lt; Sum.result;   

I expected the following output:

The sum of: 1; 1; 2; 3; 5; 8; 13; 21; 34; 55; Is: 143

but sum.result contains 0 , that is the default value specified in ctor. The only way to achieve the desired behavior is to capture the return value of for_each :

  sum = std :: for_each (a.begin (), a.end Is (yoga); Std :: cout & lt; & Lt; "Is:" & lt; & Lt; Sum.result;   

This is happening because heredity has been passed by copy instead of context:

  template   

Then the external fun is untouched, while the internal one (which is a copy of the outer) is updated and after the algorithm () is returned, the result is copied. (Or after doing all the tasks, again was removed.


There should be a good reason to work in this way, but in fact, Logic is not a design, so my questions are:

  • Sequence-operation algorithm Why are the predictions of me passed by copy instead of context?
  • Is the benefit of an approach towards pass-per-pass pass-by-reference?

    This is mostly due to historical reasons. In 1982, when all the strange stuff made it in the standard context, then there were all kinds of problems. Corps and library were resolved by DRS through C ++ 03 and above. Apart from the sensible referee-wrappers and actually working binds, they only arrive in TR1.

    People who used to try Elgo using the initial Siemens or Returns of CR ++ 98 , They can remember all kinds of trouble. Self-written algos were prone to hitting the dangerous 'context reference' problem.

    At least worked well from the price, and rarely created many problems - and to help boost the beginning of the referee and cref, where you need more.

No comments:

Post a Comment