简体   繁体   中英

Partial specialization with type nested in a templated class

I'm playing with templates and partial specialization, but there is one specialization I don't know how to write... I'll simplify code to make it easier to read.

Let's condiser

template <typename T>
    class x
{
    ...
};

Usually, I can specialize like this:

class x<a_type>
{
    ...
};

Also works with templates types:

template <typename T>
    class x<std::vector<T>>
{
    ...
}

Now I would like to make the specialization for a type nested in a templated class:

template <typename T>
    class y
{
    struct nested_type
    {
        y a_member;
    };

    ...
};

// Here comes the specialization

template <typename T>
    class x<y<T>::nested_type>
{
    ...
};

This fails. I also tried to put 'typename' before y::nested_type but it did not solved the problem. Compiler error is:

type/value mismatch at argument 1 in template parameter list for ‘template <class T> struct x’

What I want to do seems logical, but I'm not sure if it is possible. I'm using C++0x with g++-4.5. Does anybody know the correct syntax to write such specialization?

The answer is that you cannot do this specialization. It is not a syntax error, but just something that cannot be realized. You have to see template specializations a little bit like function overloading. The compiler has to take the type argument at the use-site, look at the specializations available, find matches, and select the best one (most specialized one). The problem with your example is that the "find match" step cannot be realized with such a specialization. The compiler can expect "nested_type" to be anything, not necessarily a unique type (as it is in your example), it could also be a nested typedef, for instance. Moreover, the compiler cannot predict that it is already seeing all the specializations of template "y", so even if nested_type is a unique type nested in y (general template), it could be a nested typedef in an upcoming template specialization declaration for template "y".

Just like with function overloading and the matching algorithm used there, the compiler is limited in its capabilities to deduce the type, and what limits it is how much assumptions it can make. If you have a specialization for x<int> and later use x<int> , the match is trivial, no deduction needed, no assumptions needed. If you have a specialization like x<T*> and later use x<int*> , the match is easy, T can be deduced to be int . If you have a specialization like x< y<T>::type > and then use any version of x, how is the compiler supposed to deduce T from y::type? It would have to substitute for T in y all the possible types that exist in the entire world to see if there is one that results in a matching nested type. That's an unreasonable expectation, and that is why the type deduction capabilities of C++ templates stop here. Very often, to know if you should expect the compiler to be able to resolve something, just put yourself in its shoes and see if it is even remotely possible (the answer is usually clear).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM