Q:

SetIntersection size without allocation

Given 2 sets (C++) is there a convenient way to get the size of the intersection without any alocations (as std::set_intersection does)
Sure, I could copy the implementation minus the assignment but I always rather not re-invent the wheel
int count = 0;
while (first1!=last1 && first2!=last2)
{
    if (*first1<*first2) ++first1;
    else if (*first2<*first1) ++first2;
    else {
        count++; ++first1; ++first2;
    }
 }

I was considering using std::set_intersection and pass a "counting" interator...?

A:

With some help from Boost Iterator library and C++14's generic lambdas:
#include <set>
#include <algorithm>
#include <iostream>
#include <boost/function_output_iterator.hpp>

int main()
{
    std::set<int> s1 { 1,2,3,4 };
    std::set<int> s2 { 3,4,5,6 };

    int i = 0;
    auto counter = [&i](auto){ ++i; };  // C++14
 // auto counter = [&i](int ){ ++1; };  // C++11
 // pre C++11, you'd need a class with overloaded operator()

    std::set_intersection(
        s1.begin(), s1.end(), s2.begin(), s2.end(),
        boost::make_function_output_iterator(counter)
    );

    std::cout << i;
}

Output is 2.

