help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: GURU NEEDED : macro SQUARE(x) for any type x


From: Bart van Ingen Schenau
Subject: Re: GURU NEEDED : macro SQUARE(x) for any type x
Date: Fri, 14 Jan 2011 01:57:19 -0800 (PST)
User-agent: G2/1.0

On Jan 14, 7:46 am, bolega <address@hidden> wrote:
> Basically, I have spent a few hours experimenting and searching on the
> comp.lang.c/c++
>
> Let me use SQR for brevity and saving line
>
> Here are progressively refined macros :
>
> #define SQR(x) ((x)*(x))
>
> #define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval})  // NOTE,
> closure or {} inside () is a valid idea in C, and thus no return is
> needed.

The ({ ... }) construct is not valid in standard C or standard C++. It
is a GCC extension.
If you want a standard-conforming macro that works for any type and
also yields a value of that same type, you are stuck with the first
macro you present.
There is no way to write such a macro in standard C without evaluating
x twice.

In C++, you have the option of using an inline templated function, as
shown by Gert-Jan de Vos.

If your portability needs do not extend beyond GCC (and compilers
accepting the same dialect), you can use an expression-block similar
to the second block, as shown by Rivka Miller.

>
> Bolega

Bart v Ingen Schenau


reply via email to

[Prev in Thread] Current Thread [Next in Thread]