4

A class WithTTMember has a template member type named TT.

struct WithTTMember {
    template<typename> using TT = void;
};

Another class ExpectTT takes a template template parameter:

template< template<typename> typename TT >
struct ExpectTT {};

ExpectTT<WithTTMember::TT> can be successfully instantiated.

A third class ExpectTWithTT expects a template parameter with a template member type named TT, and instantiates ExpectTT using it:

template<typename T>
struct ExpectTWithTT {
    using X = ExpectTT<typename T::TT>;  // this doesn't compile
};

I expect ExpectTWithTT<WithTTMember>::X to be the same type as ExpectTT<WithTTMember::TT>. However the code above is fails to compile.

I tried injecting the faulty line with a combination of template and typename keywords following compiler messages and my instinct, but I couldn't get it to work.

How can I express what I want?

Any C++ version is fine.

Helloer
  • 417
  • 3
  • 13

1 Answers1

5

You should use template keyword to tell that T::TT is a template.

template<typename T>
struct ExpectTWithTT {
    using X = ExpectTT<T::template TT>;
    //                    ^^^^^^^^
};
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • Oh man, that was easy! I started with `T::TT` and the compiler was telling me to add `typename`, which apparently is never good. Thanks! – Helloer Feb 18 '21 at 05:27
  • @Helloer Yes, I agree usage of these keywords is confusing. :) – songyuanyao Feb 18 '21 at 05:33
  • 1
    @Helloer You'll find this question especially helpful for reference: [Where and why do I have to put the “template” and “typename” keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – WhozCraig Feb 18 '21 at 05:36