Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class can't access its own private static constexpr method - Clang bug?

This code does not compile in Clang (6,7,8,9,trunk), but compiles just fine in GCC (7.1, 8.1, 9.1):

template<class T> struct TypeHolder { using type = T; };

template<int i>
class Outer {
private:
    template<class T> 
    static constexpr auto compute_type() {
        if constexpr (i == 42) {
            return TypeHolder<bool>{};
        } else {
            return TypeHolder<T>{};
        }
    }

public:
    template<class T>
    using TheType = typename decltype(Outer<i>::compute_type<T>())::type;
};

int main() {
    Outer<42>::TheType<int> i;
}

Clang tells me:

<source>:17:49: error: 'compute_type' is a private member of 'Outer<42>'

… which of course it is, but I'm trying to access that member from inside the same class. I don't see why it should not be accessible there. Have I hit (and should I file) a Clang bug?

You can toy around with the code at Godbolt's compiler explorer.

like image 613
Lukas Barth Avatar asked Jan 23 '20 16:01

Lukas Barth


1 Answers

This is core issue 1554. The standard is unclear how access checking is performed for alias templates (in the context of definition, or in the context of use).

The current direction is to check in the context of the definition, which would make your code well-formed.

like image 168
T.C. Avatar answered Nov 16 '22 10:11

T.C.