Вопрос

How can I test that OtherFoo template parameter (for my TT alias) is Foo with other template parameters:

template <class... Pack>
class Foo
{

    class SomeClass {};

    template <class OtherFoo> // OtherFoo = Foo with other template parameters
    using TT = typename OtherFoo::SomeClass;
};

It is assumed that it is impossible to do spoofing.

Это было полезно?

Решение

With a partial specialization, like that:

template <class... Pack>
class Foo
{
    class SomeClass {};

    template <class OtherFoo>  // OtherFoo = anything, but undefined
    struct TT_t;

    template <class... P> // OtherFoo = Foo with other template parameters
    struct TT_t <Foo <P...> > { using type = typename Foo <P...>::SomeClass };

    template <class OtherFoo> // OtherFoo = Foo with other template parameters
    using TT = typename TT_t <OtherFoo>::type;
};

So if you try to instantiate TT with any parameter other than another Foo, the compiler will issue an error, saying that TT_t is an incomplete type. I hope this is what you were after.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top