4

I want to have a priority queue with custom ordering, but lazy as I am, I don't want to define a comparator class implementing operator().

I really would like something like this to compile:

std::priority_queue<int, std::vector<int>, 
    boost::bind(some_function, _1, _2, obj1, obj2)> queue;

where some_function is a bool returning function taking four arguments, the first and second being ints of the queue, and the two last ones some objects needed for calculating the ordering (const references).

(error: ‘boost::bind’ cannot appear in a constant-expression)

But this doesn't compile. Even a more simple

std::priority_queue<int, std::vector<int>, &compare> queue;

won't compile, with compare being a binary function returning bool.

(error: type/value mismatch at argument 3 in template parameter list for ‘template class std::priority_queue’; expected a type, got ‘compare’)

Any suggestions?

1
  • You have no closing paren on the boost::bind here - before the > on the queue template params. Is that a typo in posted code or in what you tried to compile? Commented Nov 15, 2010 at 16:48

1 Answer 1

11

This could work:

std::priority_queue<int, std::vector<int>, 
    boost::function<bool(int,int)> >

Then pass in your bind expression to the queue's constructor.

P.S. you're getting those compilation errors because you're putting runtime-evaluated expressions where a typename or constant expression are expected.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, This is a good solution. I'll just note that unfortunately, this isn't exactly the same thing as statically supplying a specific template function/functor parameter, because boost::function uses dynamic allocation to create variable function objects. So, you won't get the same kind of compiler-generated inlined efficiency that you would get with a custom, statically supplied function parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.