views:

53

answers:

3

I need to provide configuration file, which will describe which STL header files to include. I have found that usually it is done by defining a lot of HAVE_XXX_HEADER macros. I wonder if there's something wrong with explicitly providing header name in a macro. Then instead of testing each variant:

#if defined(HAVE_TR1_UNORDERED_MAP_HEADER)
#include <tr1/unordered_map>
#elseif (...)
#endif

you could simply have:

#define UNORDERED_MAP_HEADER <tr1/unordered_map>
(...)
#include UNORDERED_MAP_HEADER

which in addition brings flexibility, since header name is not hard coded inside configured file.

+2  A: 

This is possible and legal in C99, cf ISO 9899:1999 §6.10.2 example 2. A similar example can also be found in the (draft) C++ standard, 16.2 bullet 8.

bdonlan
A: 

I tried doing:

#define BBB <stdlib.h>
#include BBB

And it compiles fine, using MSVS 2005. What is the problem?

ysap
The problem is that showing it works on a particular compiler doesn't imply it works on every standard-compliant compiler.
GMan
+3  A: 

Sure, you can do that. You can also use function macros; for example, I use this in my projects because gcc places TR1 headers in a subdirectory:

#ifdef __GNUC__
#  define TR1_HEADER(header) <tr1/header>
#else
#  define TR1_HEADER(header) <header>
#endif

#include TR1_HEADER(unordered_map)
#include TR1_HEADER(memory)
#include TR1_HEADER(functional)
// etc.

#undef TR1_HEADER

I have tested this on gcc and MSVC 2008. Other compilers may require other hacks, although in this particular case gcc is not strictly compliant while MSVC is, so it should work out of the box.

Anonymous Coward