Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang complains about constexpr function in case for switch statement

Tags:

c++

c++11

struct X
    {
    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:

error: case value is not a constant expression case

X::B():

However I tried gcc and it compiled fine. Anybody knows if gcc is being lenient or clang has some bug?

See on godbolt (clang 8.0.0): https://godbolt.org/z/ETe5WQ However (gcc 8.3) compiles fine (also on godbolt) and tried other versions of gcc and were also fine

Update:

opened a bug

like image 273
mkmostafa Avatar asked Mar 26 '19 14:03

mkmostafa


1 Answers

Clang (8.0.0) has a bug here.

If you write constexpr auto A = X::A(); and use case A: in your switch statement, you get the same compile error (saying that A is not a constant expression).

If you remove the cases, however, it compiles fine (which implies that A is a valid constexpr => a contradiction to the previous error).

Moreover, switch(x) fails while switch(this->x) succeeds. Since x == this->x in your case, this is definitely a bug.

As chtz mentioned, clang (5/6) seem to work just fine. That's not an argument, but an apparent regression.

Update: As mentioned by the OP, they filed a bug report.

like image 195
andreee Avatar answered Oct 20 '22 04:10

andreee