This is a contrived example, I know it doesn't make sense in this context. In my real case, v contains a larger class, and I can't refactor v due to other dependencies in the code base. Also, in the real case I'm not using std::set but it simplifies the example. Here are two example classes A and B that I know don't work, and I do understand why, but hopefully will illustrate what I'm trying to do:
#include <set>
#include <vector>
using namespace std;
struct A
{
A(initializer_list<float> init) : v(init)
{
for(size_t i = 0; i < init.size(); ++i)
{
s.insert(i);
}
}
struct Less
{
Less(const vector<float>& v) : v(v) {};
bool operator()(size_t i, size_t j)
{
return v[i] < v[j];
}
const vector<float>& v;
};
vector<float> v;
set<size_t, Less> s;
};
struct B
{
B(initializer_list<float> init) : v(init)
{
for(size_t i = 0; i < init.size(); ++i)
{
s.insert(i);
}
}
template<const vector<float>& v>
struct Less
{
bool operator()(size_t i, size_t j)
{
return v[i] < v[j];
}
};
vector<float> v;
set<size_t, Less<v>> s;
};
int main()
{
A a = {1., 2., 3.};
B b = {1., 2., 3.};
}
In both cases s contains indicies that refer to elements in v, and Less is trying to compare the values in v using those indices. Is there a way to do this that I'm not seeing?