I'm trying to use the decltype of a lambda as a template parameter.
auto compare = [](int a, int b){return a < b;};
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue;
The cppreference.com says I can do exactly this:
// From the cpprefernce.com
auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1);};
std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);
But when I try to compile I get
src/main.cpp:22:64: error: use of deleted function ‘main()::<lambda(int, int)>::<lambda>()’
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue;
^
src/main.cpp:21:18: note: a lambda closure type has a deleted default constructor
auto compare = [](int a, int b){return a < b;};
^
src/main.cpp:22:64: note: when instantiating default argument for call to std::priority_queue<_Tp, _Sequence, _Compare>::priority_queue(const _Compare&, _Sequence&&) [with _Tp = int; _Sequence = std::vector<int>; _Compare = main()::<lambda(int, int)>]
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue;
^
I don't understand why I can't do this. I'm not even trying to use the lambda's constructor am I?
I figured it out. You have to construct the priority_queue with the lambda.
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue(compare);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With