views:

46

answers:

1

My question is w.r.t the following thread : http://stackoverflow.com/questions/2009924/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?

+2  A: 

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".

GMan
+1. It could even be the case that `foo<float>` did not have an internal type `bar` at all.
David Rodríguez - dribeas
This answer begs the question though why partially specializing `bar` *is* allowed: `template<typename T> template<typename U> struct foo<T>::bar<int, U> { typedef void type; };` is all fine for a `bar` that takes *two* arguments. Now if you do `foo<float>::bar<int, char>::type` you get the same "ambiguity" :)
Johannes Schaub - litb
@Johannes: Hm, then I leave it for you. :) That's all I got.
GMan
@GMan i've no clue :(
Johannes Schaub - litb
If i were to put this in words, would it be appropriate to say this:If it was possible to specialize a nested class template, each instantiation on the outer class template would be required to have the specialized version of the nested class template which may not be necessary as there could be specializations of outer class template which may not even have, or may not wish to have the nested class in first place. Please correct me if I'm getting it wrong
hype
@hype if you explicitly specialize the outer one, and you then do `outer_specialized_one::bar<int>`, it seems clear to me that `bar<int>` is *not* the specialized one of the generic template. After all, that's all the reason why you specialize the outer one, to have it be different.
Johannes Schaub - litb
@Johannes: In that case, it would be instantiation of bar<int> within the scope of outer_specialized_one
hype