Dependent includes with modules

112 views
Skip to first unread message

Morwenn

unread,
Dec 29, 2016, 2:05:07 PM12/29/16
to SG2 - Modules
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 bar
that can be used independently, I'm unsure how I could replicate the trick above.

Is there a way to do so?

James Dennett

unread,
Dec 29, 2016, 2:34:24 PM12/29/16
to mod...@isocpp.org
My thought is: you shouldn't need to do so in a world with modules.  Unless there's more to this than meets the eye, the cost that you're worried about (parsing a lot of code each of the many times you may #include a header) shouldn't be present.

-- James

Reply all
Reply to author
Forward
0 new messages