How do I use 2 template parameters?

Oct 27, 2010 at 3:29pm
Hello everyone,

I would like to know how I can use two template parameters at once. My template-class looks something like this:

Header:
1
2
3
4
5
6
template <class T, class T2>
class A
{
	void test();
	// ...
};


cpp-file:
1
2
3
4
5
6
7
template void A<int, double>::test();
template <class T>
template <class T2>
void A<T, T2>::test()
{
	// ...
}



Now I'm getting the error:
error C2244: 'A<T,T2>::test' : unable to match function definition to an existing declaration

What am I doing wrong?

Kind regards,
Squall83
Oct 27, 2010 at 3:42pm
You can't split templates in header/source files
BTW
1
2
3
4
5
template <class T, class T2>
void A<T, T2>::test()
{
	// ...
}
Last edited on Oct 27, 2010 at 3:42pm
Oct 27, 2010 at 6:17pm
Of course I can split them. E.g. if I have a template with 1 parameter I can do the following:

Header:
1
2
3
4
5
6
template <class T>
class A
{
	void test(T input);
	// ...
};


Cpp:
1
2
3
4
5
6
7
8
9
10
#include "headerfile.h"
template void A<int>::test(int input);
template void A<char>::test(char input);
template void A<short>::test(short input);
template void A<double>::test(double input);
template <class T>
void A<T>::test()
{
    // ...
}


I could've sworn I tried your solution already and it didn't work... I'll test it tomorrow again when I'm back at work.
Oct 27, 2010 at 6:23pm
Well, if you split them, you won't be able to use the function in other cpps.
As for your original problem, I think
template <class T, class T2>
is not the same as
1
2
template <class T>
template <class T2>
Oct 28, 2010 at 6:39am
Ok, apparently I didn't try it at work properly, i.e. I had some other error which made me think that "template <class T, class T2>" wouldn't work. Now that I tried again it works. ^^

Thanks for the help! ^^

@hamsterman:
Well, if you split them, you won't be able to use the function in other cpps.

What do you mean by that? I am calling a function I implemented in a cpp file from another cpp and it works.


Oct 28, 2010 at 7:34am
This is not true for template funtions. see
http://www.cplusplus.com/doc/tutorial/templates/
scroll down to Templates and multiple-file projects.
Oct 28, 2010 at 7:42am
Do you mean this:
From the point of view of the compiler, templates are not normal functions or classes. They are compiled on demand, meaning that the code of a template function is not compiled until an instantiation with specific template arguments is required. At that moment, when an instantiation is required, the compiler generates a function specifically for those arguments from the template.

?

Because as far as I know, if I put the template function in the cpp like I did above, then the compiler generates an instantiation of the function for each type I declared. Therefore the template is resolved for those specific instantiations at compile time, the instantiations look like normal non-template functions to the compiler again and I can access them freely at runtime.
Oct 28, 2010 at 10:10am
I mean this:
Because templates are compiled when required, this forces a restriction for multi-file projects: the implementation (definition) of a template class or function must be in the same file as its declaration. That means that we cannot separate the interface in a separate header file, and that we must include both interface and implementation in any file that uses the templates.
Oct 28, 2010 at 5:17pm
But that's not true in my case. I'm including only the header in those file which need the template (i.e. no implementation at all is included) and it works.
Oct 28, 2010 at 6:04pm
Are you sure we're talking about the same thing?

example:
1
2
3
//header.h
template<typename T>
void do_something();

1
2
3
4
5
6
//code1.cpp
#include "header.h"
template<typename T>
void do_something(){
   //... implementation
}

1
2
3
4
5
6
//code2.cpp
#include "header.h"
int main(){
   do_something<int>();//will cause an error
   return 0;
}


However,
1
2
3
//header.h
template<typename T>
void do_something();

1
2
3
4
5
6
7
8
9
10
11
//code1.cpp
#include "header.h"
template<typename T>
void do_something(){
   //... implementation
}

int main(){
   do_something<int>();//everything's OK
   return 0;
}
Nov 2, 2010 at 8:12am
Well, for one thing I didn't talk about a template function, but a template class containing template functions.

Furthermore you did not create any specific instances of the function like I did in my example.
template void A<int>::test(int input); etc.
I don't know if something like that works for template functions, too, though.
Nov 3, 2010 at 6:55am
For the record:
Squall83 is correct - you can split the template into a h file and a cpp file the way he is doing it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Header:

template <class T>
class A
{
    void test(T input);
    // ...
};



Cpp:

#include "headerfile.h"
template void A<int>::test(int input); 
template void A<char>::test(char input);
template void A<short>::test(short input);
template void A<double>::test(double input);
template <class T>
void A<T>::test()
{
    // ...
}


This way is called explicit instantiation
Nov 3, 2010 at 6:58am
Explained well............
Nov 3, 2010 at 2:14pm
thanks ^^
Topic archived. No new replies allowed.