Both GCC and MSVC cannot compile.
GCC complains: invalid operands to binary expression ('typename std::remove_reference<vector<int> &>::type' (aka 'std::vector<int>') and 'std::vector<int>')
.
However, they can both compile if I explicitly write a + b
in a lambda.
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
using namespace std;
vector<int> operator+(const vector<int> &a, const vector<int> &b) {
vector<int> c;
transform(a.begin(), a.end(), b.begin(), back_inserter(c), plus());
return c;
}
int main() {
vector<vector<int>> vv{{0, 1}, {2, 3}, {4, 5}};
// vector<int> s = accumulate(vv.begin() + 1, vv.end(), vv[0]); // Error
vector<int> s = accumulate(vv.begin() + 1, vv.end(), vv[0], [](const auto &a, const auto &b) { return a + b; });
return 0;
}