Sign in to follow this  
slicer4ever

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

Recommended Posts

slicer4ever    6760
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:

[code]
#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;
}
[/code]

i hope someone can help me asap.

thanks=-)

Share this post


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


[code]

#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;
}
[/code]

Share this post


Link to post
Share on other sites
slicer4ever    6760
[quote name='ApochPiQ' timestamp='1311297260' post='4838724']
This is off the top of my head so I may be remembering wrong, but I [i]think[/i] it goes like this:

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

tried a couple of things with that thought:

first, as you said:
[code]
#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;
}
[/code]

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:
[code]#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;
}
[/code]
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
smasherprog    568
The following code doesn't mean anything, remove this code and it will compile and run correctly.


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

[/code]

Unless you wanted to do the same function without the use of a class, just a function template, in which case, change this
[code]
template<int x>
void n<float,x>::func(void){
[/code]
to this
[code]
template<class t, int x>
void func(void){
[/code]

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