Templates: Seperating Interface and Implementation

Hello,

The thought of having a declaration and it's corresponding implementation in the same file makes me sick to the stomach. I am currently building a graphics library and I wish to have a separate .h file for the declarations of the necessary classes and their implementation in a separate .cpp file, but the compiler cannot find the functions when I specify a template argument. So far I have tried "instantiating" the templates through a separate header file that is included in the .cpp file, but it doesn't seem to work. Here's a snippet of what's going on:

GraphicRoutines.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename point_t>
class Point2D
{
public:
	point_t x;
	point_t y;

	Point2D();
	Point2D(point_t pX, point_t pY);
	Point2D(const Point2D<point_t> & copy);
};

...


TemplateInstances.h
1
2
3
4
5
#include "GraphicRoutines.h"

template class Point2D<int>;

...


GraphicRoutines.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "TemplateInstances.h"

template <typename point_t>
Point2D<point_t>::Point2D()
{
	x = y = 0;
}


template <typename point_t>
Point2D<point_t>::Point2D(point_t pX, point_t pY)
{
	x = pX;
	y = pY;
}

template <typename point_t>
Point2D<point_t>::Point2D(const Point2D<point_t> & copy)
{
	x = copy.x;
	y = copy.y;
}
...


Note: the "..." are not actually in the code, I just didn't want to fill this message with all the code I have.

Is this a lost cause? The compiler cannot even find the implementation of the Point2D's default constructor during declaration (ex. Point2D<int> p; will fail). Any help would be appreciated =).

A template class should be completely defined in a single file.

People get nervous about putting compilable code in header files because it is and has been the wrong thing to do under normal circumstances, but templates don't exist until they are used. The compiler is smart enough to compile a single instance if it is parameterized the same way multiple times.

Hope this helps.
It does indeed work in a single file (that was my original set up), but I felt like experimenting with other formats. I have been trying to mimic what an old professor of mine did with his max flow program (http://www.adastral.ucl.ac.uk/~vladkolm/software/maxflow-v3.0.src.tar.gz see graph.cpp, graph.h and instances.inc). My set up is basically the same as his, yet mine fails.
Last edited on

A template class should be completely defined in a single file.



The mainstream complier just support this!


The thought of having a declaration and it's corresponding implementation in the same file makes me sick to the stomach. I am currently building a graphics library and I wish to have a separate .h file


C++ templates are a pattern used by the compiler to generate the actual classes and methods.

Just because the template has all of the graphics goodies code does not mean you have to release that to your customer.

Instead, you build a library using your templates and hand that your customer that library (now in binary) with a .h file to use the library functions (no templates in this header).

This way your key code is never seen by anyone else.
Topic archived. No new replies allowed.