Template specialization.

Started by
6 comments, last by smart_idiot 19 years, 4 months ago
Example:
template <typename X> struct Foo
 {
  template <typename Y> void bar(Y)
   {} 
 };
Your mission, should you choose to accept it, is to explain to me how to write a specialized version of Foo<X>::bar that takes an int.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Advertisement
wouldn't it just be

template <typename X> struct Foo
{
template <typename Y> void bar(Y)
{}
template <int Y> void bar(Y)
{}
};


Cheers
Chris
CheersChris
I wish. Template specialization isn't allowed to occur inside a class for some strange reason.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Do you need to specialise it instead of just overloading between a template and int?
template <typename X> struct Foo{	template <typename Y> void bar(Y)  	{	}	void bar(int);};template <typename X>void Foo<X>::bar(int){}


Enigma
what compiler are you using?
Maybe that's what I mean. I'll have to run home to my compiler.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Basically its not possiable so your only solutions is to overload that member function for ints e.g.

template <typename X>struct Foo  { template <typename Y> void bar(Y) {} void bar(int) {}};


int the version will always be invoked when an int is used,

however you can specialize a member function of specialized class template e.g.

#include <iostream>template <typename X>struct Foo  { template <typename Y> void bar(Y);};template <>template <>void Foo<double>::bar(int) { std::cout << "hi\n"; }int main() {  Foo<double> b;  b.bar(10);}
Overloading the function seems to work fine. Why didn't I think of that?

Many thankyous and ++ratings to all. Well, except snk_kid, I already rated you up as much as I could.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement