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

Started by
3 comments, last by smasherprog 12 years, 9 months ago
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=-)
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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) { /* ... */ }

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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;
}


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.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.
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){
Wisdom is knowing when to shut up, so try it.
--Game Development http://nolimitsdesigns.com: Reliable UDP library, Threading library, Math Library, UI Library. Take a look, its all free.

This topic is closed to new replies.

Advertisement