Path: utzoo!attcan!uunet!mcsun!ukc!dcl-cs!aber-cs!odin!pcg From: pcg@cs.aber.ac.uk (Piercarlo Grandi) Newsgroups: comp.lang.c++ Subject: Re: #defines.... Message-ID: Date: 7 Jul 90 15:35:34 GMT References: <9007062136.AA08003@ucbvax.Berkeley.EDU> Sender: pcg@aber-cs.UUCP Organization: Coleg Prifysgol Cymru Lines: 36 In-reply-to: jlol@ALTAR.EE.BYU.EDU's message of 6 Jul 90 21:37:14 GMT In article <9007062136.AA08003@ucbvax.Berkeley.EDU> jlol@ALTAR.EE.BYU.EDU (Jay Lawlor) writes: > With the C++ const, enum, and inline declarations, is it ever > necessary to use #define in a C++ program? Stroustrup emphatically > says in _The C++ Programming Language_ not to use them if you don't > have to. I'm wondering if you ever have to. I can't think of a > single time where you would be forced to use a #define over const or > inline... What about something like this? #define QUIT(err) { do_some_cleanup_stuff(); return(err); } To settle the matter once and for all: macros are necessary to define new syntax forms; this is often undesirable, where the new syntax is merely a travesty of the old syntax, but is in some cases unavoidable. For example there is no other way to achieve *control* abstraction, because there is no such facility in the language for it. In the Lisp and Scheme family of languages macros are used like this too, even if they they have greater facilities to do control abstraction then the lonely setjmp/longjmp of C/C++. There are other cases where syntax abstraction is also justifiable; for example you may want to implement a state machine as a switch inside a for, but then you want to hide the implementation. Outside syntax abstraction the use of the preprocessor should be avoided. -- Piercarlo "Peter" Grandi | ARPA: pcg%cs.aber.ac.uk@nsfnet-relay.ac.uk Dept of CS, UCW Aberystwyth | UUCP: ...!mcsun!ukc!aber-cs!pcg Penglais, Aberystwyth SY23 3BZ, UK | INET: pcg@cs.aber.ac.uk