Why explicit instantiation of outer class template is required before explicit instantiation of a class template

StackOverflow https://stackoverflow.com/questions/3733194

Question

My question is w.r.t the following thread : specialize a member template without specializing its parent

I'm absolutely fine with the standard saying that it is illegal to do so. But i want to understand why is it illegal to do so? What would be impact had it been allowed?

Was it helpful?

Solution

Maybe because of something like this:

template <typename T>
struct foo
{
    template <typename U>
    struct bar
    {
        typedef U type;
    };
};

template <typename T> 
struct foo<T>::bar<int> // imaginary
{
    typedef void type;
};

template <>
struct foo<float>
{
    template <typename U>
    struct bar
    {
        typedef U* type;
    };
};

// is it void [foo<T>::bar<int>] or 
// int* [foo<float>::bar<U>]?
typedef foo<float>::bar<int>::type ambiguous;

A sensible solution is to say "we'll make the entire thing explicit".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top