mardi 26 mars 2019

clang complains about constexpr function in case for switch statement

{
    enum class E
    {
        A,B
    };

    static constexpr X A()
    {
        return X{E::A};
    }

    static constexpr X B()
    {
        return X{E::B};
    }

    constexpr operator E() const
    {
        return a;
    }
    E a;
};

template <typename T>
struct Y
{
    void f()
    {
        // without this line clang errs
        const auto & x = this->x;
        switch(x)
        {
            case X::A():
            case X::B():
            default: return;
        }
    }

    X x = X::A();
};

int main()
{
    Y<int>{}.f();
}

Without the marked line in the snippet clang gives the following error, However I tried gcc and it compiled fine. Anybody knows if gcc is being lenient or clang has some bug?

error: case value is not a constant expression case

X::B():

Aucun commentaire:

Enregistrer un commentaire