"Greg London" <[email protected]> writes:

>> On Wed, Apr 17, 2013 at 09:29:34AM -0500, Greg London wrote:
>>> > You can write macros that have varargs,
>>> Dumb hardware engineer question:
>>> Why use macros when you can write a function?
>>
>> In this very simple case it makes no significant difference.
>
> When WOULD it make a difference?

When you want to do things a normal C++ expression can't that the
preprocessor can.  

E.g. I see the macros in the frameworks I use like to stick __LINE__ and
__FILE__ in error messages without you having to pass those in as extra
arguments.  And they expand the test expression both to compile it as
the target expression to evaluate and as a string constant to also stick
in error messages (by using the preprocessor's # operator).

Then macros often cover up reams and reams of boilerplate. Maybe that's
not a good reason and you could do that part of it with functions and
templates just as well.  In fact, the Aeryn framework seems to only use
the macros to do preprocessor tricks and most of their expansion is just
a one line instantiation of a template function or class that contains
the boilerplate. So I guess they preferred functions where they suffice
too (like most people?).

I was going to pass along a macro implementation from boost test or
Aeryn as an example, thinking I could find something simple and
self-contained like wrapping a statement in try and catch for a
DOES_NOT_THROW macro, but what they've done appears to be way more
compicated than that. Whatever, I'm sure you get the point.

- Mike




_______________________________________________
Boston-pm mailing list
[email protected]
http://mail.pm.org/mailman/listinfo/boston-pm

Reply via email to