I run into a dilemma when I am writing a generic toString-like template class, which gives a QString representative of common data structures that I use. I wish I can include minimal numbers of other header files in this header file (#include in .h or .c / .cpp?), but also wish to have it not rely on the order it is included (Header file order).
The basic code is like this:
// Some headers
template <typename T>
class PMsg
{
// Public routines
public:
static QString fromType();
static QString fromValue(T const &val);
}; // PMsg
// Inline definition of all methods in PMsg<T>::fromValue
// Some specialisation
// To be continued...
And here comes the problems: container classes. Should I do the following to pulling all container headers to make it work:
// To the beginning
#include <QtCore/QList>
#include <QtCore/QVector>
// After PMsg<T>
template <typename T>
class PMsg<QList<T> >
{
// Public routines
public:
static QString fromType();
static QString fromValue(QList<T> const &val);
}; // PMsg
// Some definitions
template <typename T>
class PMsg<QVector<T> >
{
// Public routines
public:
static QString fromType();
static QString fromValue(QVector<T> const &val);
}; // PMsg
// Some definitions
Or to use macros to detect what headers are included as:
#if defined(QLIST_H)
template <typename T>
class PMsg<QList<T> >
{
// Public routines
public:
static QString fromType();
static QString fromValue(QList<T> const &val);
}; // PMsg
// Some definitions
#endif
#if defined(QVECTOR_H)
template <typename T>
class PMsg<QVector<T> >
{
// Public routines
public:
static QString fromType();
static QString fromValue(QVector<T> const &val);
}; // PMsg
// Some definitions
#endif
I might eventually include more than 20 different headers (including some non-Qt classes like those in Eigen), or I might rely on something Qt can change without telling downstream developers and ask my colleagues to include Qt headers before this header for sure.
I am also not sure how much effect on compiling time both ways would cause at this time (which probably should not be considered if I haven't get to this problem).
Is there any other better way to solve this or which way should I follow?
Sorry for the bad question title and thank you for your help.
I think I need to provide more information. We have a downstream class called PLogger which use a template method to call PMsg::fromValue() and to write down messages. If I get it right, I need all specialisation included in that class, and this is the main usage of PMsg class. I doubt whether seperating specialisations would help. – nocte107 Aug 24 '14 at 13:53