Jump to content
  • Advertisement
Sign in to follow this  
slicer4ever

[c++] template specilization, and multiple template parameters.

This topic is 2557 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 everyone, i'm hoping someone can help me solve my problem with partial template specialization, i've broken the problem down into this simple program:


#include <stdio.h>

template<class t, int x>
class n{
void func(void){
printf("default class t, x: %d\n", x);
}
};

template<> //i've tried: template<,int x>(error with ','), and several other implementations, all unsuccesful
void n<float,x>::func(void){
printf("float class, x: %d\n", x);
}

int main(int argc, char **argv){
n<int,5> x; x.func();
n<float,10> y; y.func();
return 0;
}


i hope someone can help me asap.

thanks=-)

Share this post


Link to post
Share on other sites
Advertisement
This is off the top of my head so I may be remembering wrong, but I think it goes like this:

template <typename T>
void n<float, T>::func(void) { /* ... */ }

Share this post


Link to post
Share on other sites
This is not a template function, this is a template class. The following compiles.




#include <stdio.h>

template<class T, int X>
class n
{
public:
void func() { printf("default class t, x: %d\n", X); }
};

template<int X>
class n<float,X>
{
public:
void func() { printf("float class, x: %d\n", X); }
};

int main(int argc, char **argv)
{
n<int,5> x;
x.func();

n<float,10> y;
y.func();

return 0;
}

Share this post


Link to post
Share on other sites

This is off the top of my head so I may be remembering wrong, but I think it goes like this:

template <typename T>
void n<float, T>::func(void) { /* ... */ }



tried a couple of things with that thought:

first, as you said:

#include <stdio.h>

template<class t, int x>
class n{
void func(void){
printf("default class t, x: %d\n", x);
}
};

template<typename x>
void n<float,x>::func(void){
printf("float class, x: %d\n", x);
}

int main(int argc, char **argv){
n<int,5> x; x.func();
n<float,10> y; y.func();
return 0;
}


but this gives an error of:
Line 11: error: type/value mismatch at argument 2 in template parameter list for 'template<class t, int x> class n'

then:
#include <stdio.h>

template<class t, int x>
class n{
void func(void){
printf("default class t, x: %d\n", x);
}
};

template<int x>
void n<float,x>::func(void){
printf("float class, x: %d\n", x);
}

int main(int argc, char **argv){
n<int,5> x; x.func();
n<float,10> y; y.func();
return 0;
}

which gives an error of:
Line 11: error: invalid use of undefined type 'class n<float, x>'

@A Brain in a vat, thanks, i didn't realize i had to add that extra bit for class specialization, thanks.

Share this post


Link to post
Share on other sites
The following code doesn't mean anything, remove this code and it will compile and run correctly.



template<int x>
void n<float,x>::func(void){
printf("float class, x: %d\n", x);
}



Unless you wanted to do the same function without the use of a class, just a function template, in which case, change this

template<int x>
void n<float,x>::func(void){

to this

template<class t, int x>
void func(void){

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!