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.

Recommended Answers

All 2 Replies

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.

commented: Great effort to explain some advanced concepts :D +2

I appreciate your effort!

Your explanation helped!
Thanks!

( reputation up for you! )

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.