This is probably a case of Friday afternoon blindness... why is the accumulation over the map not working. It hands me a compile error
main.cpp:35:80: note: no known conversion for argument 2 from ‘std::pair, std::unique_ptr >’ to ‘const std::pair, std::unique_ptr >&’
What is wrong with this cast?
#include <vector>
#include <map>
#include <memory>
#include <numeric>
#include <iostream>
using namespace std;
struct Foo
{
long bar() const {return 1;}
};
int main()
{
std::vector<std::unique_ptr<Foo>> v;
auto baz = [](long x, const std::unique_ptr<Foo>& p){return x + p->bar();};
std::accumulate(v.begin(), v.end(), 0L, baz);
std::map<std::string, std::unique_ptr<Foo>> m;
auto bam = [](long x, const std::pair<std::string, std::unique_ptr<Foo>>& p) {
return x + p.second->bar();
};
std::accumulate(m.begin(), m.end(), 0L, bam);
return 0;
}