我正在按照 this 答案定义一个带有 lambda 函数的 priority_queue。但是,我正在运行:错误:未评估上下文中的 lambda 表达式

#include <bits/stdc++.h>

int main()
{
    std::priority_queue<
        int,
        std::vector<int>,
        decltype( [](int a, int b)->bool{
                   return a>b;
        })>
         q;
}

最佳答案

您的代码是有效的 C++20,但无效的 C++11。

在 C++20 之前的未计算上下文(例如 decltype )中不允许使用

  • Lambda 表达式。
  • 闭包类型在 C++20 之前不可默认构造。在 C++20 中,没有捕获的闭包类型是默认可构造的。
  • 关于C++:未计算上下文中的 lambda 表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52734311/

    10-11 22:38
    查看更多