Mistake on using pointers-to-members in Clang

Viewed 88

The following code compiles gcc successfully

template<typename T>
class F {
public:
    template<typename V>
    static void foo(V T::*) {
        p<bool> = nullptr;
    }

    template<typename V>
    static inline V T::* p;
};

struct A;
template class F<A>;

int main() {
}

and clang compilation fails with

<source>:10:18: error: member pointer refers into non-class type 'bool'

        static inline V T::* p;

                        ^

<source>:6:3: note: in instantiation of static data member 'F::p' requested here

                p<bool> = nullptr;

                ^

https://godbolt.org/z/NX7VmH

is this invalid code or i don't understand clang?

1 Answers

The code is valid from a pure language perspective. That is a valid variable template, and the resulting variable is itself of a valid type (a pointer to member). As a matter of fact, Clang complains even without the explicit instantiation.

And to drive the point home, Clang has no problem with this variable template if you move the p<bool> expression outside of the template. It's a bug in Clang that causes it to reject your code.

Related