C ++ template specialization does not work with nested types

The following code compiles, but does not work:

template<typename T>
struct Nesting
{
    template<typename U>
    struct _Nested
    {
    };

    template<typename U>
    using Nested = _Nested<U>;
};

template<typename T>
struct F
{
    static constexpr bool is_my_nested_class = false;
};

template<typename T, typename U>
struct F<typename Nesting<T>::Nested<U>>
{
    static constexpr bool is_my_nested_class = true;
};

I create these types of Nesting and Nested and try to use a type type template on it. It compiles (using MSVC 2014 w / CPP11), but

F<Nesting<int>::Nested<long>>::is_my_nested_class

returns false.

Is it prohibited or undefined by standard? Which rule does it violate? Any workaround?

Many thanks!

+4
source share
1 answer

A nested alias can refer to any type, especially by specialization:

template<typename T>
struct Nesting
{
    template<typename U>
    struct _Nested
    {
    };

    template<typename U>
    using Nested = _Nested<U>;
};

// Consider this specialisation:
template<>
struct Nesting<int>
{
    template<typename U>
    using Nested = float;
};

Now it’s clear what F<Nesting<int>::Nested<int>>::is_my_nested_classshould be the same as F<float>::is_my_nested_classhow can the compiler do this for the latter case? That is, if I wrote:

static_assert(F<float>::is_my_nested_class, "not nested");

, F<float> F<Nesting<int>::Nested<int>>, . , .

+1

Source: https://habr.com/ru/post/1679134/


All Articles