Skip to content

Erroneous if constexpr instantiation in lambda template #44851

Open
@llvmbot

Description

@llvmbot
Bugzilla Link 45506
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @hubert-reinterpretcast,@zygoloid

Extended Description

clang++ erroneously instantiates the if constexpr statement commented "pfft" below; g++ and VS2019 accept it on local consensus is that it is a clang++ bug:

#include <cstdint>
#include <type_traits>
#include <variant>
#include <iostream>

template <typename T>
T foo(const std::variant<int64_t, double, std::string>& v)
{
    T result;
    std::visit([&result](auto&& a)
    {
        typedef std::decay_t<decltype(a)> vt;
        if constexpr (std::is_same_v<vt, double>)
            result = static_cast<T>(a);
        else if constexpr (std::is_same_v<vt, int64_t>)
            result = static_cast<T>(a);
        else if constexpr (std::is_same_v<vt, std::string> && std::is_class_v<T>)
            result = T::from_string(a); // pfft
        else
            throw std::logic_error("wibble");
    }, v);
    return result;
}

int main()
{
    std::cout << foo<double>(int64_t{});
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++17clang:frontendLanguage frontend issues, e.g. anything involving "Sema"confirmedVerified by a second partyconstexprAnything related to constant evaluationlambdaC++11 lambda expressions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions