[OpenIndiana-discuss] weird C++ problem

James Carlson carlsonj at workingcode.com
Mon Apr 1 19:31:34 UTC 2013


On 04/01/13 15:03, Apostolos Syropoulos wrote:
>> I think that begs a bit of a question: why would you want to include any
>> header file (even one in your own application) within a namespace {}
>> block?  That sounds to me like an open invitation to hard-to-understand
>> problems.
>>
> 
> Removing the namesace definition in the simple example solves the problem but 
> 
> when applying this solution to the real file, the compiler complains about
> many other things. The only way to compile the original file is to put00
> 
> the following lines at the beginning of the input file:
> 
> 
> #undef __cplusplus
> #define __cplusplus 1

At a wild guess, that may have something to do with the stray 'extern
"C"' block as well.

I don't quite know what you're trying to do, but when I write C or C++
code, I try hard to make sure that my header files are correct when
included stand-alone (that is, they resolve any dependency issues
internally and don't depend on inclusion order) and that they assume
only that they're included from the top level context of the source
file.  I think that doing this:

extern "C" {
#include "my-file.h"
}

would be nearly as wrong as doing this:

int myarray[] = {
#include "my-file.h"
};

I call it "wrong" because it causes the text-based file inclusion to
occur in a really surprising context, and one which the header file
designer almost certainly could not have imagined.  And surprises are
sort of the opposite of maintainable code.

But, as I said, I don't know what you're doing.  I suppose it's possible
that you have a Good Reason for doing this, and it's just my lack of
imagination that prevents me from seeing it.  ;-}

-- 
James Carlson         42.703N 71.076W         <carlsonj at workingcode.com>



More information about the OpenIndiana-discuss mailing list