28

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.

4
  • 3
    Apparently adding friend int main(); stops Clang from complaining. Commented Jan 23, 2020 at 16:29
  • 2
    Funny! However, the access check should definitely be made with the "permissions" of Outer<42>, not main - right? Looking even more like a bug to me now. Commented Jan 23, 2020 at 16:32
  • Does using std::result_of work instead?
    – Brandon
    Commented Jan 23, 2020 at 16:32
  • FWIW, Also works in ICC and MSVC.
    – ChrisMM
    Commented Jan 23, 2020 at 16:33

1 Answer 1

23

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.