Skip to content

[C++20] Lambdas in unevaluated contexts: wrong conversion from lambda to std::priority_queue #492

@adrian-stanciu

Description

@adrian-stanciu

C++ Insights link: https://cppinsights.io/s/f1515a7d

void f()
{
    std::priority_queue<int,
                        std::vector<int>,
                        decltype([](int lhs, int rhs) { return lhs > rhs; })> min_heap;
}

translates into

void f()
{
    
    class __lambda_8_34
    {
        ...
    };
  
    std::priority_queue<int, std::vector<int, std::allocator<int> >, __lambda_8_34> min_heap = __lambda_8_34{};
}

which is not valid C++ code:

error: conversion from 'f()::__lambda_8_34' to non-scalar type 'std::priority_queue<int, std::vector<int, std::allocator<int> >, f()::__lambda_8_34>'

A slightly modified function, where the lambda is defined before the std::priority_queue:

void g()
{
    auto cmp_greater = [](int lhs, int rhs) { return lhs > rhs; };
    std::priority_queue<int,
                        std::vector<int>,
                        decltype(cmp_greater)> min_heap;
}

translates into

void g()
{
    
    class __lambda_13_24
    {
        ...
    };
  
    __lambda_13_24 cmp_greater = __lambda_13_24{};
    std::priority_queue<int, std::vector<int, std::allocator<int> >, __lambda_13_24> min_heap = std::priority_queue<int, std::vector<int, std::allocator<int> >, __lambda_13_24>();
}

which is valid C++ code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions