Skip to main content
gamedev.net
🔒 Locked

Template specialization.

Started by smart_idiot Nov 24, 2004 at 10:19 AM 6 replies 1.2k views
Original Post
smart_idiot
smart_idiot
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::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
chollida1
chollida1
wouldn't it just be

template struct Foo
{
template void bar(Y)
{}
template void bar(Y)
{}
};


Cheers
Chris
CheersChris
smart_idiot
smart_idiot
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
Enigma
Enigma
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
smart_idiot
smart_idiot
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
snk_kid
snk_kid
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);}
smart_idiot
smart_idiot
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.