vendredi 25 janvier 2019

Template (.tpp) file include guards

When writing templated classes, I like to move the implementation into a different file (myclass.tpp) and include it at the bottom of the main header (myclass.hpp).

My Question is: Do I need include guards in the .tpp file or is it sufficient to have them in the .hpp file?

Example code:

myclass.h

#ifndef MYCLASS_H
#define MYCLASS_H

template<typename T>
class MyClass
{
public:
    T foo(T obj);
};

//include template implemetation
#include "myclass.tpp"

#endif

myclass.tpp

#ifndef MYCLASS_TPP //needed?
#define MYCLASS_TPP //needed?

template<typename T>
T MyClass<T>::foo(T obj)
{
    return obj;
}

#endif //needed?

Aucun commentaire:

Enregistrer un commentaire