I'm unable to find wording in the standard forbidding using a reference type as the comparison function. Thus it seems that this would be legal. Note that some things, such as default constructing such a set, will be forbidden because your comparison type is not default constructable.
Finally note that the canonical C++ approach is to not do this, but to maintain the state externally. When you take that approach it's totally clear what you're doing and guaranteed to be safe:
#include <set>
#include <iostream>
struct A {
int& i_;
explicit A(int& state) : i_(state) { }
bool operator()(int a, int b)
{
++i_;
return a < b;
}
};
int main() {
int i;
std::set<int, A> s1( {1, 2, 3}, A(i));
std::set<int, A> s2( {4, 5, 6}, A(i));
std::cout << i << endl;
}