1,105,625 Community Members

Class Template Specialization <typename T, int Size>

Member Avatar
MrEARTHSHAcKER
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi,

I am learning templates so I have one question for this code:

template<typename T, int Number>
class Data{
      T Array[Number];
      public:
      T& operator[](int);
      };
      
template <typename T,int Number>
T& Data<T,Number>::operator[](int element){
                                  return Array[element];
                                  }

I wish to create specialization for that class template, for any random data type, but my knowledge allows me only to create class template specialization for template with only one parameter .

So I don't understand how'd the code look for class template specialization which contains expression parameter ( int Number ).

Thanks.

Member Avatar
mike_2000_17
21st Century Viking
4,084 posts since Jul 2010
Reputation Points: 2,253 [?]
Q&As Helped to Solve: 800 [?]
Skill Endorsements: 73 [?]
Moderator
Featured
Sponsor
 
1
 

It's actually simpler than you think (for once!). To do a partial template specialization in C++ you simply leave the unspecified template arguments in the template declaration and put them in the class name... well, that sounds weird, so let me just put it in code:

// This class-template has 2 arguments <T,Number>.
template<typename T, int Number>
class Data {            // This class-template is a general template 
                        //  because 'Data' appears unspecified (just 'Data').
    T Array[Number];
  public:
    T& operator[](int);
};
      
template <typename T,int Number>
T& Data<T,Number>::operator[](int element){
  return Array[element];
};


//this class template has only one argument <Number>
template <int Number>
class Data<double, Number> {    // This class-template is a specialization of 'Data' 
                                //  because 'Data' appears specified as 'Data<double,Number>'.
    double Array[Number];
  public:
    double& operator[](int);
};
      
// The member function of Data<double,Number> is simply as follows:
template <int Number>
double& Data<double,Number>::operator[](int element){
  return Array[element];
};

Once you understand the logic behind it, it's quite simple to understand. And the fact that template arguments are types or integral values, or even template template arguments, doesn't make much of a difference.

So, it's as simple as that keep the unspecified arguments in the template declaration ('template < .. >') and specify the other arguments in the class-name (while filling in the remaining arguments with the new template arguments).

You can actually do much more interesting things once you understand how this all works. For instance, these are also a valid partial specialization:

template <typename T, int Number>
class Data< Data<T, Number>, Number> {
  ...  some kind of square matrix (or tensor, shall I say) ...
};

template <typename T, typename Allocator, int Number>
class Data< std::vector<T, Allocator>, Number> {
  ... 
};

Of course, if you abuse this too much you will eventually find that there are limits to what a compiler can resolve, and also that template specializations can be ambiguous just like function overloads can be ambiguous (the selection rules are more or less analogous).

IBM has some nice pages on template specialization.

Member Avatar
MrEARTHSHAcKER
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I appreciate your effort!

Your explanation helped!
Thanks!

( reputation up for you! )

Question Answered as of 2 Years Ago by mike_2000_17
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article