In such a code:
Comparator comp(3);
set<string, Comparator> s1(comp);
set<string, Comparator> s2(comp);
set<string, Comparator> s3(comp);
set<string, Comparator> s4(comp);
the actual instance of the Comparator (namely comp) is copied at each creation of a set object as the cpp reference states
The container keeps an internal copy of alloc and comp, which are used to allocate storage and to sort the elements throughout its lifetime.
So we were wondering if this is legal in C++
#include <set>
#include <iostream>
struct A {
int i = 0;
bool operator()(int a, int b)
{
++i;
return a < b;
}
};
int main()
{
A a;
std::set<int, A&> s1( {1, 2, 3}, a);
std::set<int, A&> s2( {4, 5, 6}, a);
std::cout << a.i;
}
Thanks in advance.