This code builds fine with gcc and it used to build fine with VC2008 but it causes a compiler error with VS2019. The original code used more complicated object types so the comparisons look quite clumsy for a simple int - however the error is still the same:-
Code:
#include <set>
struct compare_fn
{
bool operator()(const int* p1, const int* p2)
{
return *p1 < *p2;
}
};
typedef std::set<int* > OrigList;
#if 0
typedef std::set<int*, compare_fn> SortedList;
#else
typedef std::set<int* > SortedList;
#endif
void
create_sorted_list(const OrigList& orig_list, SortedList& sorted_list)
{
for (OrigList::const_iterator i = orig_list.begin(); i != orig_list.end(); ++i) {
sorted_list.insert(*i); // <--- ERROR AT THIS LINE !!
}
}
As the code stands, it compiles fine - but that's only because the definitions for OrigList and SortedList are identical. So try changing #if 0 to be #if 1
What's supposed to happen is that sorted_list.insert(*i); copies objects into a new set via in a call to compare_fn - and apparently this should work - but VS2019 gives a (slightly strange) compiler error:-
Code:
error C3848: expression having type 'const compare_fn' would lose some const-volatile qualifiers in order to call 'bool compare_fn::operator ()(const int *,const int *)'
Can anyone suggest a way to make this work? At one stage I tried removing all the const modifiers but IIRC that just produced a different error.