4

I am following this answer to define a priority_queue with a lambda function. However, I am running to: error: lambda-expression in unevaluated context

#include <bits/stdc++.h>

int main()
{
    std::priority_queue<
        int,
        std::vector<int>,
        decltype( [](int a, int b)->bool{
                   return a>b;
        })>
         q;
}
T.C.
  • 133,968
  • 17
  • 288
  • 421
AspiringMat
  • 2,161
  • 2
  • 21
  • 33
  • 2
    First please read [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – Some programmer dude Oct 10 '18 at 06:55
  • As for your problem, the simple solution is to just use a variable for the lambda. As shown in the answer you link to. You need that anyway since you need to pass the comparator "function" as an argument to the `std::priority_queue` constructor. – Some programmer dude Oct 10 '18 at 06:57
  • @Someprogrammerdude I usually don't use it. But I am just practicing STL so it is more convenient if you just wanna play with it. I don't use it in large projects. – AspiringMat Oct 10 '18 at 06:57
  • 1
    Ohh! I think I see my problem, I didn't pass the comparator lambda to the constructor! Missed that in the example above! – AspiringMat Oct 10 '18 at 07:01

1 Answers1

13

Your code is valid C++20 as written but invalid C++11.

  • Lambda expressions are not allowed in unevaluated contexts (such as decltype) before C++20.
  • Closure types are not default constructible before C++20. In C++20 a closure type that has no capture is default constructible.
T.C.
  • 133,968
  • 17
  • 288
  • 421