On 16/11/16 23:52, Andreas wrote:
> David Brown <
david...@hesbynett.no> writes:
>
>> Changing the meaning of an include file depending on the definition of a
>> macro is evil, and that is the root of the problem here.
>
> While I agree to your considerations about macros, this statement seems
> to be a little too broad for me to buy it.
>
> I think it is quite common to change the meaning of include files
> depending on a macro definition. Some examples are:
>
> * macros which expand to declspec(dllimport) or declspec(dllexport) (or
> corresponding __attribute__ annotations) depending on from where they
> are included
I would say that sounds evil too. But I have not worked with C++ (or C)
on Windows, at least not for writing dlls, so I can't say if there is a
better way to do it. Maybe I would call it a "necessary evil".
> * adding or removing declarations (like typedefs) to/from include files
> depending on macro deinitions
>
> For instance glibc headers are full of those.
>
Let me be a little narrower and say that I think changing the meaning
/within a program/ of an include file depending on the definition of a
macro is evil. Changing the details depending on things like
pre-defined macros from the compiler is fair enough - the glibc headers
do that in order to be portable across a range of targets. And changing
depending on compiler flags (such as "-D" flags to define macros used
for options) is also okay, if it is well documented. It is also fine to
have something like an "options.h" file that is included by other
headers and used to change the effects of those headers.
What I don't like is when the meaning of the same header-defined
identifier (type, function, macro, whatever) is different in different
parts of the code, because of the order of #defines and #includes. It
makes things inconsistent, and hard to follow, and leads to incorrect
assumptions.
(And to be clear, just because something is "evil" does not mean it
should not be used. Sometimes evil practices are unavoidable, or the
alternatives are even worse.)