Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

C++ partical class member template functions.

This topic is 2592 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

hello all, i'm trying to achieve the following:


#include <stdio.h>

template<class A, class B>
class x{
public:
void m(void){
printf("Default M!\n");
return;
}
};

template<>
void x<int, int>::m(void){
printf("Int/Int M!\n");
return;
}

/*
template<class A>
void x<int, A>::m(void){
printf("Int/A M!\n");
return;
}*/

int main(int argc, char **argv){
x<float, float> *y = new x<float, float>;
x<int, int> *z = new x<int, int>;
//x<int, float> *w = new x<int, float>;
y->m();
z->m();
//w->m();
delete y;
delete z;
//delete w;
return 0;
}


basically, trying to partially specialize based on what one type is, and not what the other, without having to specialize the entire class, when only one/few functions of the class require specialization.

Share this post


Link to post
Share on other sites
Advertisement
You can't partial specialize a template function, you can only overload it.

For your case, a solution is to define a functor class to handle the real function, then forward m to that functor.


template <typename T, typename U>
struct RealM
{
static void DoM() { default M, aha }
};

template <>
struct RealM <int, int>
{
static void DoM() { int and int, aha }
};

template <typename T>
struct RealM <int, T>
{
static void DoM() { int and T, aha }
};

template<class A, class B>
class x{
public:
void m(void){
RealM<A, B>::DoM();
}
};

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!