Splitting templates

Hi,

I read that splitting templates into .h / . cpp wasn't a good idea.

Is the only way to keep a clean code (read organized and clear) is to include the templates in separate txt files ?

Since those template class don't really need to be splitted in .h and . cpp like actual classes.

Like so :
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include "testy.txt"
using namespace std;



int main(int argc, char *argv[])
{

    Bilou <double>obj(54);
    Bilou <int>obj2(54);
    Bilou <char>obj3('q');
    int c = obj2.tiger();
    int q = obj3.tiger();

    cout << c << endl << q << endl;


    return 0;
}


and testY.txt :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T>
class Bilou {
    private :
    T a;

    public :
    Bilou(T a){};
    int tiger();

};


template <>
int Bilou<char>::tiger() {

return 1;}

template <class T>
int Bilou<T>::tiger(){
return 2;}


Is there any performance issue I cannot imagine in doing so ?
Is it the right way to do ?

Thanks !

Larry
> Is it the right way to do ?

Yes.

Though you would want to rename the C++ header file "testy.txt" as "testy.h" (or "testy.hpp")

And the header should have an include guard.
http://en.wikipedia.org/wiki/Include_guard
Last edited on
According to your advice, I've just done that and works like a charm.

Thanks JL.

But I don't understand the fuss about the cpp files though, if it is so simple to keep it all in a header (?!)

Edit : I mean that on the net lot of people complain about problems with separated templates (h/cpp) during compile time.

But I think they just learn as I do.

Many thanks,

Larry
Last edited on
> But I don't understand the fuss about the cpp files though,
> if it is so simple to keep it all in a header (?!)

As the size of the code base becomes larger, it is not so simple. We would want to separate the declaration from the implementation for a variety of reasons. For example,

If we make a small change to the implementation, we wouldn't want to recompile and retest every file that used the component.

We might want to package the implementation into a separate library which contains the already compiled code and provide the library along with a set of headers to the users of the library. For example, the header <regex> contains the declarations of various regex functions, and libc++ contains their implementations.

This model - called 'the separate compilation model' - is essential to promote modularity and loose coupling.
http://www.informit.com/articles/article.aspx?p=26039


However, the separate compilation model can't be used with templates; a template definition is not code in the classical C sense. With templates, a class or a functions is created when the template is instantiated; to be able to instantiate the template, the compiler needs to see the definition of the template at compile time.

http://www.parashift.com/c++-faq-lite/templates-defn-vs-decl.html
Last edited on
Many Thanks JL,

Now I got it. To answer this, I will split my code into several pieces.

Cheers !

Larry
Topic archived. No new replies allowed.