Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Parameter passing to std::accumulate

My reference is to the example provided under the following:

std::accumulate

I am trying to constrast the following usage of std::next in the example,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

std::string s = 
  std::accumulate(
    std::next(v.begin()), 
    v.end(),
    std::to_string(v[0]), // start with first element
    dash_fold
  );

with the usage example provided for std::next

Given that in the referred to example for std::accumulate usage, the following is the signature of dash_fold:

auto dash_fold = [](std::string a, int b){...}

I am assuming that the construct std::next(v.begin()), v.end(), std::to_string(v[0]...
above should produce

int, string

which is reversing the order of parameters that dash_fold expects; is this understanding correct? Or am I missing something?

TIA

>Solution :

The referenced manual is very detailed.

constexpr T accumulate( InputIt first, InputIt last, T init,
                        BinaryOperation op );

T in your case is the type of std::to_string(v[0]), is std::string.

op:

Ret fun(const Type1 &a, const Type2 &b);

The type Type1 must be such that an object of type T can be implicitly converted to Type1.

Type1 must be such that an object of type std::string can be implicitly converted to Type1. If the first parameter type is int, then std::string cannot be implicitly converted to int – fail. If the first parameter type is std::string, then it works well.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading