问题
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 ?
回答1:
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).
回答2:
You cannot do this partial specialization indeed, but there is a workaround which you can use the achieve desired effect.
Instead of specialization, force the target type to implement what you need.
Here is a minimal example for that:
#include <vector>
#include <iostream>
#include <cassert>
// Default template defines an interface against the target type.
template <class T> struct Traits
{
using TraitsType = typename T::foo_type;
static void foo()
{
T::foo();
}
};
// This is the sample class.
template <class T> struct MyStuff
{
struct NestedType
{
int x;
// It implements the desired features.
using foo_type = int;
static void foo()
{
std::cout << "Using Nested version!\n";
}
};
};
// For built in types you can use specialization.
template <> struct Traits<int>
{
using TraitsType = double;
static void foo()
{
std::cout << "Using int version.\n";
}
};
//... If you can't touch the nested type, the you are SOL.
int main()
{
static_assert(std::is_same<Traits<int>::TraitsType, double>::value);
static_assert(std::is_same<Traits<MyStuff<int>::NestedType>::TraitsType, int>::value);
static_assert(std::is_same<Traits<MyStuff<double>::NestedType>::TraitsType, int>::value);
Traits<int>::foo(); // Prints "Using int version"
Traits<MyStuff<int>::NestedType>::foo(); // Prints "Using Nested version!\n"
Traits<MyStuff<double>::NestedType>::foo(); // Prints "Using Nested version!\n"
return 0;
}
来源:https://stackoverflow.com/questions/6114185/partial-specialization-with-type-nested-in-a-templated-class