macros: type save, or what?

Janice Caron caron800 at googlemail.com
Tue Sep 11 12:33:43 PDT 2007


On 9/11/07, 0ffh <spam at frankhirsch.net> wrote:
>
> I don't see why a const should be different from a literal when it comes
> to type checking, and a literal is what you'll effectively get, when you
> use "macro a=5;" or whatever.

Type-checking may be a red herring then.

It just feels /right/ to me that you should use
const int x = 42;

to declare a constant. It feels natural. It's what instinct tells me to do.

At least with const, specifying the type explicitly is optional. Both
of these will compile:
const x = 42;
const int x = 42;

But if macro becomes the "official" way to declare constants then you
don't get the option to specify the type, because "macro int x = 42;"
won't compile.



> Anyways, this tread goes a bit beyond what we had in "Const sucks", that
> is why I gave it its own name... :)

True. I /like/ macros. They can be used for complex and powerful
abstractions. But if all you want to do is declare a constant, they're
overkill. They're not the right tool for the job.

(I prefer the keyword "define" to "macro" though) :-)



More information about the Digitalmars-d mailing list