Why can't I build a std :: set with the creation of a predicate, but can I assign a std :: set constructed in this way?

So, I have an array (0 - n) containing the values ​​that I want to use std :: set to sort it. An unsigned array of int cost [n].

I use the following functor for this sort:

struct ProxySorter { ProxySorter(const unsigned* arr) : proxy_array(arr) {} bool operator()(const unsigned& a, const unsigned& b) const { return proxy_array[a] < proxy_array[b]; } const unsigned* proxy_array; }; 

So, here is the problem ... When I create a set, it is legal:

 std::set<unsigned, ProxySorter> node_queue = std::set<unsigned, ProxySorter>(ProxySorter(cost)); 

I get no errors and everything works as expected. But it seems hacky and messy.

However, this is apparently illegal:

 std::set<unsigned, ProxySorter> node_queue(ProxySorter(cost)); 

Trying to simply construct it using ProxySorter (cost) causes a lot of errors saying such things for every call to a set of elements:

error: request to delete an element in node_queue that is of type non-class std :: set <unsigned int, ProxySorter> (ProxySorter)

What is the problem with the usual design? Why does the job work? What is the difference here that I am missing? Any help is greatly appreciated, thanks.

Oh, and sorry about the title of the question, I was not sure what to call it.

+8
c ++ set constructor stl predicate
source share
1 answer

The most annoying parsing . You need two more parentheses:

 std::set<unsigned, ProxySorter> node_queue((ProxySorter(cost))); 

Otherwise, it will be interpreted as declaring a function that returns your set type and accepts an argument of type ProxySorter called cost .

+13
source share

All Articles