In one of my libraries, I use a trick to potentially reduce the code the compiler has to parse when the full library isn't used.
For example, let's say I have two headers, foo.h and bar.h that can be used independently, which define two template classes,
foo<T> and
bar<T>.
When both are used at once, a specialization
foo<bar<T>> exists to provide some useful optimizations. However, this specialization is quite expensive,
and I want it to be included only when foo.h and bar.h have been included. In order to do that, I have the following files:
foo.h
#ifndef FOO_H
#define FOO_H
template<typename T>
struct foo
{
// whatever...
};
#ifdef BAR_DONE_
#include "foobar.h"
#endif
#define FOO_DONE_
#endif
bar.h
#ifndef BAR_H
#define BAR_H
template<typename T>
struct bar
{
// whatever...
};
#ifdef FOO_DONE_
#include "foobar.h"
#endif
#define BAR_DONE_
#endif
foobar.h
#ifndef FOOBAR_H
#define FOOBAR_H
template<typename T>
struct foo<bar<T>>
{
// expensive code to parse
};
#endif FOOBAR_H
With that preprocessor trick, I can avoid including the expensive specialization when it's clear that it won't be used anyway.I plan to switch to change the library in a near future to use modules, once they're widely available, but given two modules
foo and
barthat can be used independently, I'm unsure how I could replicate the trick above.
Is there a way to do so?