Template and Class Specialisation

Hello! I'm quite curious about the format of specialised templates. Here's one:
1
2
3
  template <typename X, typename Y>
  void Display (X &, Y&)
  {...};

I know this is the normal template. But when it comes to specialised templates...
1
2
  template <> void Display<X>(X&, Y&) // I'm not sure if I could use a Y here.
  {...};

I don't understand what is that "<X>" for in this case. I'm reading a book and it says it tells the complier that it is a template for X, but I don't get it? I thought the complier chooses what is best (based on the arguments) to use?

One more thing...
1
2
  template <class A> class A<T*>
  {...};

What does it mean by partial specialisation?

Thank you!
What book is it?

Drakonaut wrote:
I don't understand what is that "<X>" for in this case

Neither would the compiler:
test.cc:7:26: error: use of undeclared identifier 'X'

Explicit (or "full") specialization is when you explicitly specify every template parameter. In your case, the base template has two type template parameters (X and Y), so to fully specialize it, you must name two known types to be used in their place:
template<> void Display<std::string, int>(std::string& x, int& y) { ...

More detail here:
http://en.cppreference.com/w/cpp/language/template_specialization

Partial specialization allows you to constrain template parameters: if your primary template was template<class T> class A {...}; then you can define template<class T> class A<T*> {...}; for all pointer types: A<int> would use the first definition, A<int*> would use the second one.

More detail here http://en.cppreference.com/w/cpp/language/partial_specialization
You defined the first template with 2 parameters, so you need to also define the specialization with 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

template <typename X, typename Y>
void Display (X &x, Y &y)
{
  std::cout << "X: " << x << ", y: " << y << std::endl;
}

template <>
void Display<float,float>(float &x,float &y) // display decimal values for fp
{
  std::cout << "X: " << std::fixed << x << ", y: " << std::fixed << y << std::endl;
}

int main()
{
  float x = 1.0, y = 4.0;
  int ix = 1, iy = 4;
  Display(x,y);
  Display(ix,iy);
  return 0;
}
C++ Primer Plus (6th Edition)

What I mean was what is the use of including the "<X, Y>" (my mistake, my bad)? In the book it says that it can be removed if you spell it out like this:

 
template <> void Show<job> (job&, job&)

where job is a struct, to
 
template<> void Show(job&, job&)

The book says in the prototype it can be removed, but never state in the definition whether it can be removed or not. So I was wondering what it meant in the "<>". Is it to indicate the types that can be used in the function template?

Thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

template < typename X, typename Y > void foo( const X&, const Y& )
{ std::cout << "the general case: foo<X,Y>\n" ; }

// both template type arguments are explicitly specified
template <> void foo<int,int>( const int&, const int& )
{ std::cout << "explicit specialisation: foo<int,int>\n" ; }

// the first template type argument is explicitly specified, the second is deduced from context
template <> void foo<char>( const char&, const char& )
{ std::cout << "explicit specialisation: foo<char,char>\n" ; }

// both template type arguments are deduced from context
template <> void foo( const short&, const short& ) 
{ std::cout << "explicit specialisation: foo<short,short>\n" ; }

// this is not an explicit specilisation. This is an overload of the function foo
// favour overloading function templates (over explicitly specialising function templates)
// see: http://www.gotw.ca/publications/mill17.htm
void foo( const double&, const double& )
{ std::cout << "overload: foo( const double&, const double& )\n" ; }

int main()
{
    foo( 56UL, 71UL ) ; // the general case: foo<X,Y>

    foo( 12, 78 ) ; // explicit specialisation: foo<int,int>
    foo( 'a', 'b' ) ; // explicit specialisation: foo<char,char>
    foo( short(12), short(12) ) ; // explicit specialisation: foo<short,short>

    foo( 2.3, 5.6 ) ; // overload: foo( const double&, const double& )
}

http://coliru.stacked-crooked.com/a/0dd338e11654bc21
So "<>" simply means that it tells the complier the arguments that the template specialises? And including it or not doesn't make much a different because it can be deduced by the complier?

Yea, I found a thread that includes that link you posted "http://www.gotw.ca/publications/mill17.htm".

Thank you for your answers! :D
Topic archived. No new replies allowed.