Jump to content
  • Advertisement
Sign in to follow this  
Nokturnal

Templates

This topic is 3167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to follow thinking in c++. I have not yet seen a declaration as such: template<class T> class C<T, double> Can some one please help me understand this? Thanks!
//: C05:PartialOrder2.cpp
// Reveals partial ordering of class templates.
#include <iostream>
using namespace std;
 
template<class T, class U> class C {
public:
  void f() { cout << "Primary Template\n"; }
};
 
template<class U> class C<int, U> {
public:
  void f() { cout << "T == int\n"; }
};
 
template<class T> class C<T, double> {
public:
  void f() { cout << "U == double\n”; }
};
 
template<class T, class U> class C<T*, U> {
public:
  void f() { cout << "T* used\n”; }
};
 
template<class T, class U> class C<T, U*> {
public:
  void f() { cout << "U* used\n”; }
};
 
template<class T, class U> class C<T*, U*> {
public:
  void f() { cout << "T* and U* used\n”; }
};
 
template<class T> class C<T, T> {
public:
  void f() { cout << "T == U\n”; }
};
 
int main() {
  C<float, int>().f();    // 1: Primary template
  C<int, float>().f();    // 2: T == int
  C<float, double>().f(); // 3: U == double
  C<float, float>().f();  // 4: T == U
  C<float*, float>().f(); // 5: T* used [T is float]
  C<float, float*>().f(); // 6: U* used [U is float]
  C<float*, int*>().f();  // 7: T* and U* used [float,int]
  // The following are ambiguous:
//   8: C<int, int>().f();
//   9: C<double, double>().f();
//  10: C<float*, float*>().f();
//  11: C<int, int*>().f();
//  12: C<int*, int*>().f();
} ///:~

Share this post


Link to post
Share on other sites
Advertisement
template<class T> class C<T, double> is a class template partial specialization. Let me give you an example:

template <typename T, unsigned int N>
struct MyStaticArray {}; // 0) primary template

template <typename T>
struct MyStaticArray<T, 16> {}; // 1) partial specialization for the case where N == 16

template <unsigned int N>
struct MyStaticArray<bool, N> {}; // 2) partial specialization for the case where T == bool

template <>
struct MyStaticArray<int, 16> {}; // 3) specialization for T=int, N=16

// For sake of example, I omitted their definitions

int main () {

MyStaticArray<float,32> w; // invokes the primary template 0)
MyStaticArray<float,16> x; // invokes the partial specialization 1)
MyStaticArray<bool,32768> y; // invokes the partial specialization 2)
MyStaticArray<int,16> z; // invokes the full specialization 3)
}


For a complete guide, I'd recommend "C++ Templates: The Complete Guide" by Josuttis and Vandevoorde. It is also partially on google-books.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!