This is the mail archive of the cygwin-talk mailing list for the cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: how come #include "*.cpp" works?


On Wed, 24 May 2006, Dave Korn wrote:

> On 24 May 2006 17:00, mwoehlke wrote:
>
> > === For declaring an object
> > #define CREATABLE_CLASS(name) \
> > extern class OBJECT* create_##name( int argc, const char* argv[] ); \
> > class name : virtual public OBJECT
>
> > === So main.cpp doesn't need the headers for every class
> > #undef CREATABLE_CLASS
> > #define CREATABLE_CLASS(name) \
> > extern class T_OBJ* create_##name( int argc, const char* argv[] );
> > #include "objects.def"
>
>   Bad idea, I think.  You now have two separate bits of code that are
> generating the prototypes for this set of functions.  You run the danger
> of getting inconsistent code.  In this case, unless T_OBJ is the exact
> same type as OBJECT, you've already made (technically speaking)
> undefined behaviour, haven't you?  Umm, perhaps I mean "Would someone
> remined me whether C++ mangled names include the return type, or just
> the args signature?"

The mangled name doesn't, but C++ will invoke different coercions based
on the return type, so if OBJECT and T_OBJ are indeed different types,
calling the function in exactly the same way from main.cpp and another
file may result in completely different behavior.

>   Perhaps it would be better to factor out the prototype generation into
> a separate macro that could be invoked by both instances of "#define
> CREATABLE_CLASS" ?

What's wrong with the following?

#define _CREATABLE_CLASS_DECL(name) \
class name : virtual public OBJECT
#define _CREATABLE_CLASS_PROTO(name) \
extern class OBJECT* create_##name( int argc, const char* argv[] );

#ifndef CC_NO_CLASS_DECL
#define CREATABLE_CLASS(name) \
_CREATABLE_CLASS_PROTO(name) \
_CREATABLE_CLASS_DECL(name)
#else
#define CREATABLE_CLASS(name) \
_CREATABLE_CLASS_PROTO(name)
#endif

and then, in main.cpp:

#define CC_NO_CLASS_DECL
#include "objects.def"

	Igor
-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_	    pechtcha@cs.nyu.edu | igor@watson.ibm.com
ZZZzz /,`.-'`'    -.  ;-;;,_		Igor Peshansky, Ph.D. (name changed!)
     |,4-  ) )-,_. ,\ (  `'-'		old name: Igor Pechtchanski
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"Las! je suis sot... -Mais non, tu ne l'es pas, puisque tu t'en rends compte."
"But no -- you are no fool; you call yourself a fool, there's proof enough in
that!" -- Rostand, "Cyrano de Bergerac"


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]