geraldCelente
geraldCelente

Reputation: 995

c++ - Template type parameter type

I have a templated class ParameterTemplateClass which is a parameter type to another templated class.

Basically my code looks as below.

template <class T1, class T2>
    class ParameterTemplateClass
    { 
      typedef T1 Type1;
      typedef T2 Type2;
    };


    template <ParameterTemplateClass<template<class T1, class T2> > >
    class SomeClass
    { 
      typedef typename ParameterTemplateClass::Type1 Type1;
      typedef typename ParameterTemplateClass::Type1 Type1;
    };

template<>
class SpecializedClass::ParameterTemplateClass<int, float>
{ }

template<>
class SomeSpecializedClass::SomeClass<SpecializedClass>
{ }

I cannot get this to work. I have tryed different approaches, including the approach shown at What are some uses of template template parameters in C++?; however I have not been successful so far.

Please not that I want class SomeClass template parameter to be ParameterTemplateClass, and not T1, T2 which are parameter types for ParameterTemplateClass.

Can this be achieved? Can somebody please let me know. Thanks

Upvotes: 1

Views: 163

Answers (1)

hazydev
hazydev

Reputation: 344

Please not that I want class SomeClass template parameter to be ParameterTemplateClass, and not T1, T2 which are parameter types for ParameterTemplateClass.

This is not a semantic that templates handle -- can you give a more detailed example?

If you want to refer to the ParameterTemplateClass, then explicitly use it inside of SomeClass, for example,

template <class T1, class T2 >
class SomeClass
{ 
    ParameterTemplateClass <T1, T2> foo;
};

EDIT:

If you want to have SomeClass take a templated class, this can be expressed such as:

template <template <class T1, class T2> class T>   class SomeClass
{ 
};

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8l.doc%2Flanguage%2Fref%2Ftemplate_template_arguments.htm

EDIT2:

If SomeClass should not be aware of T1 and T2, then implement it as:

 template <class T>   
 class SomeClass
 { 
 };

And use it as: SomeClass<ParameterTemplateClass <T1, T2> > Templates do not have the semantic of "limiting" to only use ParameterTemplateClass. If you want a compile time error raised if `SomeClass<...> is passed a class other than ParameterTemplateClass, there are tricks that can do this but there is no clear motivation for using these in this context.

EDIT 3:

Modified based on edits to the question. You can express the typedefs as:

template <class T>
class SomeClass
{ 
  typedef typename T::Type1 Type1;
  typedef typename T::Type2 Type2;
};

Upvotes: 4

Related Questions