* GURU NEEDED : macro SQUARE(x) for any type x
@ 2011-01-14 6:46 bolega
2011-01-14 7:09 ` luser- -droog
` (5 more replies)
0 siblings, 6 replies; 13+ messages in thread
From: bolega @ 2011-01-14 6:46 UTC (permalink / raw)
To: help-gnu-emacs; +Cc: bolega
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.
this macro is given in several posts like
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2b108e325da6/e5fe657622e5595b?q=%22typedef+xtype%22&pli=1
there is a problem with typedef
Bolega
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
@ 2011-01-14 7:09 ` luser- -droog
2011-01-14 7:10 ` Rivka Miller
` (4 subsequent siblings)
5 siblings, 0 replies; 13+ messages in thread
From: luser- -droog @ 2011-01-14 7:09 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 14, 12:46 am, bolega <gnuist...@gmail.com> 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.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega
What problem?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
2011-01-14 7:09 ` luser- -droog
@ 2011-01-14 7:10 ` Rivka Miller
2011-01-14 7:15 ` luser- -droog
` (3 subsequent siblings)
5 siblings, 0 replies; 13+ messages in thread
From: Rivka Miller @ 2011-01-14 7:10 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 13, 10:46 pm, bolega <gnuist...@gmail.com> 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.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega
try if this works
#define square(x) ({typeof(x) temp=x; temp*temp;})
Rivka
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
2011-01-14 7:09 ` luser- -droog
2011-01-14 7:10 ` Rivka Miller
@ 2011-01-14 7:15 ` luser- -droog
2011-01-14 8:14 ` Hans Vlems
2011-01-14 8:51 ` Gert-Jan de Vos
` (2 subsequent siblings)
5 siblings, 1 reply; 13+ messages in thread
From: luser- -droog @ 2011-01-14 7:15 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 14, 12:46 am, bolega <gnuist...@gmail.com> 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.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega
OIK
#define SQR(x) pow((x),2)
or
#define SQR(x) exp(2 * log(x))
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 7:15 ` luser- -droog
@ 2011-01-14 8:14 ` Hans Vlems
2011-01-14 9:04 ` luser- -droog
0 siblings, 1 reply; 13+ messages in thread
From: Hans Vlems @ 2011-01-14 8:14 UTC (permalink / raw)
To: help-gnu-emacs
On 14 jan, 08:15, luser- -droog <mijo...@yahoo.com> wrote:
> On Jan 14, 12:46 am, bolega <gnuist...@gmail.com> 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.
>
> > this macro is given in several posts like
>
> >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> > there is a problem with typedef
>
> > Bolega
>
> OIK
>
> #define SQR(x) pow((x),2)
> or
> #define SQR(x) exp(2 * log(x))- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -
What happens for x=0 in the second example?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
` (2 preceding siblings ...)
2011-01-14 7:15 ` luser- -droog
@ 2011-01-14 8:51 ` Gert-Jan de Vos
2011-01-14 11:13 ` James Kuyper
2011-01-14 9:57 ` Bart van Ingen Schenau
2011-01-15 2:24 ` PKM
5 siblings, 1 reply; 13+ messages in thread
From: Gert-Jan de Vos @ 2011-01-14 8:51 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 14, 7:46 am, bolega <gnuist...@gmail.com> 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.
>
> this macro is given in several posts like
>
> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> there is a problem with typedef
>
> Bolega
Why not just an inline function template (this being c.l.c++)?
template <typename T>
inline T square(T value)
{
return value*value;
}
Gert-Jan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 8:14 ` Hans Vlems
@ 2011-01-14 9:04 ` luser- -droog
0 siblings, 0 replies; 13+ messages in thread
From: luser- -droog @ 2011-01-14 9:04 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 14, 2:14 am, Hans Vlems <hvl...@freenet.de> wrote:
> On 14 jan, 08:15, luser- -droog <mijo...@yahoo.com> wrote:
>
>
>
> > On Jan 14, 12:46 am, bolega <gnuist...@gmail.com> 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.
>
> > > this macro is given in several posts like
>
> > >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>
> > > there is a problem with typedef
>
> > > Bolega
>
> > OIK
>
> > #define SQR(x) pow((x),2)
> > or
> > #define SQR(x) exp(2 * log(x))- Tekst uit oorspronkelijk bericht niet weergeven -
>
> > - Tekst uit oorspronkelijk bericht weergeven -
>
> What happens for x=0 in the second example?
Rats! And you can't do a ternary 'cause you'd have to
mention x twice.
But that would be a penalty in lisp, too wouldn't it?
An expression with a side-effect would perform the
side-effect twice, right?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
` (3 preceding siblings ...)
2011-01-14 8:51 ` Gert-Jan de Vos
@ 2011-01-14 9:57 ` Bart van Ingen Schenau
2011-01-15 2:24 ` PKM
5 siblings, 0 replies; 13+ messages in thread
From: Bart van Ingen Schenau @ 2011-01-14 9:57 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 14, 7:46 am, bolega <gnuist...@gmail.com> 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
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 8:51 ` Gert-Jan de Vos
@ 2011-01-14 11:13 ` James Kuyper
2011-01-14 20:01 ` Stefan Monnier
0 siblings, 1 reply; 13+ messages in thread
From: James Kuyper @ 2011-01-14 11:13 UTC (permalink / raw)
To: help-gnu-emacs
On 01/14/2011 03:51 AM, Gert-Jan de Vos wrote:
> On Jan 14, 7:46�am, bolega<gnuist...@gmail.com> 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.
>>
>> this macro is given in several posts like
>>
>> http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/38ef2...
>>
>> there is a problem with typedef
>>
>> Bolega
>
> Why not just an inline function template (this being c.l.c++)?
>
> template<typename T>
> inline T square(T value)
> {
> return value*value;
> }
Because this is also cross-posted to c.l.c. That could mean he's one of
those people who thinks C and C++ are a single language called C/C++,
but alternatively, it could mean that he needs a separate solution for
each language, or a single solution that works in both languages.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 11:13 ` James Kuyper
@ 2011-01-14 20:01 ` Stefan Monnier
2011-01-14 21:57 ` James Kuyper
0 siblings, 1 reply; 13+ messages in thread
From: Stefan Monnier @ 2011-01-14 20:01 UTC (permalink / raw)
To: help-gnu-emacs
> Because this is also cross-posted to c.l.c. That could mean he's one of
> those people who thinks C and C++ are a single language called C/C++, but
> alternatively, it could mean that he needs a separate solution for each
> language, or a single solution that works in both languages.
Since he also cross-posted to g.e.help, I guess we should presume he
thinks Help is also a language in the same family or that he also wants
a solution in that language?
Stefan
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 20:01 ` Stefan Monnier
@ 2011-01-14 21:57 ` James Kuyper
0 siblings, 0 replies; 13+ messages in thread
From: James Kuyper @ 2011-01-14 21:57 UTC (permalink / raw)
To: help-gnu-emacs
On 01/14/2011 03:01 PM, Stefan Monnier wrote:
>> Because this is also cross-posted to c.l.c. That could mean he's one of
>> those people who thinks C and C++ are a single language called C/C++, but
>> alternatively, it could mean that he needs a separate solution for each
>> language, or a single solution that works in both languages.
>
> Since he also cross-posted to g.e.help, I guess we should presume he
> thinks Help is also a language in the same family or that he also wants
> a solution in that language?
I have not the slightest idea why he cross-posted to g.e.help. The
speculation I wrote about his motives for posting to c.l.c and c.l.c++
reflects my past experiences with other people who made similar
cross-postings.
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
` (4 preceding siblings ...)
2011-01-14 9:57 ` Bart van Ingen Schenau
@ 2011-01-15 2:24 ` PKM
2011-01-15 19:55 ` Keith Thompson
5 siblings, 1 reply; 13+ messages in thread
From: PKM @ 2011-01-15 2:24 UTC (permalink / raw)
To: help-gnu-emacs
On Jan 13, 11:46 pm, bolega <gnuist...@gmail.com> wrote:
> #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.
I don't see how this could possibly work:
/*test.c*/
#define SQR(x) ({typedef xtype=x; xtype xval=x; xval*xval})
int main(void)
{
SQR(2);
return 0;
}
$ gcc test.c
test.c: In function ‘main’:
test.c:7: error: typedef ‘xtype’ is initialized (use __typeof__
instead)
test.c:7: error: expected ‘;’ before ‘}’ token
Recompiling after making those changes yields
test.c: In function ‘main’:
test.c:7: error: expected ‘(’ before ‘xtype’
test.c:7: error: ‘xtype’ undeclared (first use in this function)
test.c:7: error: (Each undeclared identifier is reported only once
test.c:7: error: for each function it appears in.)
test.c:7: error: expected ‘;’ before ‘xval’
test.c:7: error: ‘xval’ undeclared (first use in this function)
And even if it did compile, isn't it the case that
this code is attempting to use the same token as both
a value and as a type?
^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: GURU NEEDED : macro SQUARE(x) for any type x
2011-01-15 2:24 ` PKM
@ 2011-01-15 19:55 ` Keith Thompson
0 siblings, 0 replies; 13+ messages in thread
From: Keith Thompson @ 2011-01-15 19:55 UTC (permalink / raw)
To: help-gnu-emacs
PKM <phoenixstormcrow@gmail.com> writes:
> On Jan 13, 11:46 pm, bolega <gnuist...@gmail.com> wrote:
>> #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.
>
> I don't see how this could possibly work:
It can't.
It depends on two gcc-specific extensions, and it gets both of them
wrong.
Here's a version that actually works with gcc:
#define SQR(x) ({typedef typeof(x) xtype; xtype xval=(x); xval * xval;})
The "typeof" keyword is non-standard, as is the use of a "statement
expression".
There is no general solution in standard C that avoids evaluating the
argument twice.
But in practice, this:
#define SQR(x) ((x) * (x))
works perfectly well. The convention of using all-caps for macro names
warns the user that the argument might be evaluated more than once.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2011-01-15 19:55 UTC | newest]
Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-14 6:46 GURU NEEDED : macro SQUARE(x) for any type x bolega
2011-01-14 7:09 ` luser- -droog
2011-01-14 7:10 ` Rivka Miller
2011-01-14 7:15 ` luser- -droog
2011-01-14 8:14 ` Hans Vlems
2011-01-14 9:04 ` luser- -droog
2011-01-14 8:51 ` Gert-Jan de Vos
2011-01-14 11:13 ` James Kuyper
2011-01-14 20:01 ` Stefan Monnier
2011-01-14 21:57 ` James Kuyper
2011-01-14 9:57 ` Bart van Ingen Schenau
2011-01-15 2:24 ` PKM
2011-01-15 19:55 ` Keith Thompson
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.