Sign in to follow this  
smart_idiot

Template specialization.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this