Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to prevent a header file without header guards from being included at all at compile time?

I am trying to ban the inclusion of a list of header files from a source file, and thus preventing the dependency on classes defined in those headers.

There are tricks of checking the header guard macros (like the following), but, in my case, the headers to exclude all use #pragma once instead of header guards.

#ifdef A_BANNED_HEADER_FILE_H
static_assert(false);
#endif

Is there a way to enforce 0 inclusion at compile time?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

P.S. For the purpose of avoiding dependency to any class, I could define a class of the same qualified name in the source file and let redefinition error scream at accidental inclusion.

>Solution :

Preventing a header from being included doesn’t necessarily prevent any class from being defined.

But you can explicitly demand that a class is not defined in your source file.

// foo.h must not be included, because class foo must not be defined!
// #include "foo.h"


template<typename T> concept is_defined = requires { sizeof(T); };


static_assert( !is_defined<class foo> );

See it work in Compiler Explorer

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading