Wednesday, 4 September 2013

Set default template parameters of an inner template class using template parameters of the outer class

Set default template parameters of an inner template class using template
parameters of the outer class

Targeting Visual Studio C++ 2008
The situation: I have a template class with a large number of parameters,
many with defaults.
template <typename A, typename B = b, typename C = c>
struct Outer
{
typedef typename A typeA;
typedef typename B typeB;
typedef typename C typeC;
};
So good so far. Now, I have a user defined type consisting of lots and
lots of Outers. In this situation, the types A and B are known, C is not.
My first approach with dealing with this was just to replicate A and B
inside the new user type.
template<typename A, typename B>
struct UserDefinedType {
Outer<A, B, int> AnIntOuter;
Outer<A, B, int> AFloatOuter;
};
This works, but gets boring rather quickly. (as well as other complexities
from the real code). I was thinking... why not create a new inner class
using the template parameters that was passed in as default values, so I
tried doing this:
template<typename A, typename B>
struct AnotherUserDefinedType {
template<typename CC, typename AA = A, typename BB = B>
struct Inner : public Outer<AA, BB, CC> {};
Inner<int> AnIntInner;
Inner<float> AFloatInner;
};
When I try compiling this, I get a 'too few template arguments' error
which seems to be attached to the declaration of the member (AnIntInner in
this case).
What I want to know: Is this (using template parameters of the outer class
as default template parameters of the inner class) even remotely possible?
If it IS possible, is my construction wrong or are there known issues with
MSVC++ 2008? Or, of course, if I probably have something else wrong in my
code as well

No comments:

Post a Comment