Skip to content

lambda expression is not a constant expression when auto (generic) parameter is used. #35052

Open
@llvmbot

Description

@llvmbot
Bugzilla Link 35704
Version trunk
OS Windows NT
Attachments sample cpp file
Reporter LLVM Bugzilla Contributor

Extended Description

When constexpr lambda used in constant expression it going non constant when its parameter is auto.

In this sample got an error:

1> ConsoleApplication1.cpp(20,16): error : constexpr if condition is not a constant expression
1> if constexpr (f(TypeTag::value))
1> ^~~~~~~~~~~~~~~~~~~~

But if

auto predicate = [](auto v) constexpr -> bool

replaced by

auto predicate = [](size_t v) constexpr -> bool

all ok.


#include

template
struct TypeTag
{
constexpr static size_t value = 1;
};

template<>
struct TypeTag
{
constexpr static size_t value = 2;
};

template<typename T, typename F>
constexpr auto func(F f)
{
if constexpr (f(TypeTag::value))
{
return int{1};
}
else
{
return float{2.2f};
}
}

int main()
{
auto predicate = [](auto v) constexpr -> bool // does not compile when v is auto
{
return v == 1;
};

constexpr auto ok = predicate(75);

constexpr auto fail = func<int>(predicate);

return 0;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++17clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluationlambdaC++11 lambda expressionsrejects-valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions