Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: lambda-expression in unevaluated context

Tags:

c++

c++11

stl

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;
}
like image 348
AspiringMat Avatar asked Oct 17 '25 06:10

AspiringMat


1 Answers

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.
like image 170
T.C. Avatar answered Oct 19 '25 21:10

T.C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!