A very common coding practice is to separate the interface of a class from the implementation of its member functions through the use of .h and .cpp files on a per-class basis. So class Foo would be realised with a Foo.h header file and a corresponding Foo.cpp file.
This is often thrown out of the window in the special case of generic classes and instead header-only libraries are used to keep the compiler happy even though it does clutter the interface file with implementation details.
I've recently come accross some code written as follows. The .h file contains the interface and a #include to a .hpp file which contains the implementation of the generic member functions.
e.g. for a simple container of type T
Value.h
#ifndef VALUE_H
#define VALUE_H
template <typename T>
class Value
{
public:
Value(T value);
void set(T value);
T get() const;
private:
T data;
};
#include "Value.hpp"
#endif
and the corresponding Value.hpp
#ifndef VALUE_HPP
#define VALUE_HPP
template <typename T>
Value<T>::Value(T value) : data(value)
{
}
template <typename T>
void Value<T>::set(T value)
{
data = value;
}
template <typename T>
T Value<T>::get() const
{
return data;
}
#endif
This has the advantage of better separating interface and implementation coupled with the further benefit of actually compiling (in my limited testing).
My question is then are there any hidden pit-falls with adopting this convention?
inline, templates or not they are still in a header file. - john