我正在尝试使用lambda的decltype作为模板参数.
auto compare = [](int a, int b){return a < b;};
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue;
cppreference.com说我可以做到这一点:
// 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);
但是当我尝试编译时,我得到了
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;
^
我不明白为什么我不能这样做.我甚至都没有尝试使用lambda的构造函数吗?
解决方法:
我想到了.您必须使用lambda构造priority_queue.
std::priority_queue<int, std::vector<int>, decltype(compare)> my_queue(compare);