Reputation: 23
I am trying to obtain a nested template class.
The compiler gives me the following error:
error: type/value mismatch at argument 1 in template parameter list for ‘template class MyClass’ note: expected a class template, got ‘SpecificType {aka TemplateClass< Type1, Typ2, Type3 >}
This is my code
/* ...class definition... */
template < template < typename T1,typename T2,typename T3 > typename T>
class MyClass{
// is there a way to have these types available here
// passing one single specialized type in the main?
T1 member1;
T2 member2;
T3 member3;
T<T1, T2, T3> member4;
};
/* ...in my main... */
using SpecificType = TemplateClass< Type1, Type2, Type3 >;
SpecificType test_object; //until here compiles
MyClass< SpecificType > myclass;
I have already tried to define a SpecificType object, and until that point my code compiles and works.
Upvotes: 2
Views: 453
Reputation: 25663
You define MyClass
to accept a template as parameter and not a type. As this you simply should pass the template e.g. TemplateClass
itself.
So your definition says: I want to have a template which itself has three template parameters. So you must pass a template and not a instance of a template.
/* ...class definition... */
template < template < typename T1,typename T2,typename T3 > typename T>
class MyClass{ /*...*/};
template <typename T1, typename T2, typename T3>
class TemplateClass {};
/* ...in my main... */
int main()
{
MyClass< TemplateClass > myclass;
}
After passing the template to your class, inside the class you can use the template to instantiate it like:
template < template < typename T1,typename T2,typename T3 > typename T>
class MyClass
{
T<int, float, char> obj;
};
After getting your comment, you want a different thing! You want to pass a type, here you want the instance of a template. So simply do the following:
/* ...class definition... */
template < typename T>
class MyClass
{
T obj;
};
template <typename T1, typename T2, typename T3>
class TemplateClass {};
int main()
{
MyClass< TemplateClass< int, float, char> > myclass;
}
And now to get the second request from OP :-)
It is possible to pass a type ( e.g. here we see the instantiated template ) and get out the types from the template parameters. Look here:
template < typename T>
class MyClass;
template < template < typename ,typename ,typename > typename T, typename T1, typename T2, typename T3>
class MyClass< T<T1,T2,T3> >
{
T1 var1; // here you can use the types...
T2 var2;
T<T1,T2,T3> obj;
};
template <typename T1, typename T2, typename T3>
class TemplateClass {};
int main()
{
MyClass< TemplateClass< int, float, char> > myclass;
}
Upvotes: 4