Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Shao Miller <sha0.miller@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: macro |
| Date | 2012-01-20 10:50 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <jfc2jq$eoo$1@dont-email.me> (permalink) |
| References | (7 earlier) <lnaa5myr89.fsf@nuthaus.mib.org> <9nmh3nFaaoU1@mid.individual.net> <8a04a8ba-eee0-48fd-9cb6-98695875840e@f33g2000yqh.googlegroups.com> <9nnjboFaaoU3@mid.individual.net> <20120118130142.908@kylheku.com> |
On 1/18/2012 16:09, Kaz Kylheku wrote:
>[...]
> Here is a requirement:
>
> or(x, y)
>
> This must evaluate X. If X is nonzero, then the value X is the result, and Y
> is not evaluated. Otherwise the result is the value of Y. X is evaluated
> exactly once, and Y at most once.
>
> Good luck making a function for this. Or even an entirely clean macro,
> for that matter.
> [...]
Well, I don't know a lot about C++, but in C1X, I think you might be
able to get close with the code below. I think it could be simplified a
lot if there was a C '_TypeOf'!
I've commented out the '_Thread_local' as I guess that's for C > C1X.
Some restrictions apply:
- There are only certain, specified, supported types for the 'M_OR'
macro arguments.
- If the 'M_OR' macro invocation is indeterminately sequenced relative
to another 'M_OR' macro invocation, the behaviour is undefined.
- Without '_Thread_local', use of 'M_OR' is not thread-safe.
- Other stuff I'm forgetting at the moment, but you folks will notice,
I'm sure.
Here is the code:
#include <stddef.h>
#include <stdio.h>
#define M_COPY_OF_TYPES_(u_, m_) \
u_(unsigned char, m_) \
u_(signed char, m_) \
u_(char, m_) \
u_(unsigned short, m_) \
u_(signed short, m_) \
u_(unsigned int, m_) \
u_(signed int, m_) \
u_(unsigned long, m_) \
u_(signed long, m_) \
u_(float, m_) \
u_(double, m_)
#define M_COPY_OF_CASE_(t_, m_) \
t_: \
(t_[1]) { _Generic(m_, t_: m_, default: 0) },
#define M_COPY_OF(x_) ( \
_Generic( \
(x_), \
M_COPY_OF_TYPES_(M_COPY_OF_CASE_, (x_)) \
default: \
0 \
) \
)
#if 0
_Thread_local static void * or_helper_;
#endif
static void * or_helper_;
#define M_OR_HELPER_GET_CASE_(t_, m_) \
t_: \
(t_ *) or_helper_,
#define M_OR_HELPER_GET_(x_) ( \
_Generic( \
(x_), \
M_COPY_OF_TYPES_(M_OR_HELPER_GET_CASE_, (x_)) \
default: \
0 \
) \
)
#define M_OR_HELPER_SET_(x_) ( \
(or_helper_ = M_COPY_OF(x_)), \
M_OR_HELPER_GET_(x_) \
)
#define M_OR(x_, y_) ( \
*M_OR_HELPER_SET_(x_) ? *M_OR_HELPER_GET_(x_) : (y_) \
)
int main(void) {
int x;
int * ip;
float y, z;
float * fp;
puts("M_COPY_OF demonstrations:");
x = 42;
ip = M_COPY_OF(x++);
printf("x == %d && *ip == %d\n", x, *ip);
ip = M_COPY_OF(13);
printf("*ip == %d\n", *ip);
fp = M_COPY_OF(3.14f);
printf("*fp == %f\n", *fp);
puts("");
x = 2;
y = 3.14;
printf("Initial conditions: x == %d && y == %f\n", x, y);
z = M_OR(x--, y++);
printf(
"After first M_OR: x == %d && y == %f && z == %f\n",
x,
y,
z
);
z = M_OR(x--, y++);
printf(
"After second M_OR: x == %d && y == %f && z == %f\n",
x,
y,
z
);
z = M_OR(x--, y++);
printf(
"After third M_OR: x == %d && y == %f && z == %f\n",
x,
y,
z
);
return 0;
}
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:29 -0800
Re: macro Dr Nick <3-nospam@temporary-address.org.uk> - 2012-01-15 13:44 +0000
Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 05:58 -0800
Re: macro "Joachim Schmitz" <nospam.jojo@schmitz-digital.de> - 2012-01-15 15:34 +0100
Re: macro "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-01-15 06:37 -0800
Re: macro dspfun <dspfun@hotmail.com> - 2012-01-15 07:28 -0800
Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:30 -0800
Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 00:18 -0800
Re: macro Hans Vlems <hvlems@freenet.de> - 2012-01-16 00:52 -0800
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 22:08 +1300
Re: macro Nick Keighley <nick_keighley_nospam@hotmail.com> - 2012-01-16 03:04 -0800
Re: macro Markus Wichmann <nullplan@gmx.net> - 2012-01-16 12:42 +0100
Re: macro jgk@panix.com (Joe keane) - 2012-01-16 14:48 +0000
Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-16 12:01 -0800
Re: macro Philip Lantz <prl@canterey.us> - 2012-01-17 01:02 -0800
Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-17 13:05 -0800
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 13:12 +1300
Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 01:05 -0800
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-18 22:57 +1300
Re: macro "osmium" <r124c4u102@comcast.net> - 2012-01-18 06:22 -0600
Re: macro dspfun <dspfun@hotmail.com> - 2012-01-18 07:50 -0800
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 14:09 +1300
Re: macro blp@cs.stanford.edu (Ben Pfaff) - 2012-01-18 11:28 -0800
Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 21:09 +0000
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:08 +1300
[OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 15:58 +1300
Re: [OT] Re: macro Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-01-20 12:22 +0000
Re: [OT] Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-21 07:58 +1300
Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-19 22:47 -0500
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 17:06 +1300
Re: macro James Kuyper <jameskuyper@verizon.net> - 2012-01-20 00:12 -0500
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-20 18:28 +1300
Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-20 03:21 -0800
Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 10:50 -0500
Re: macro Shao Miller <sha0.miller@gmail.com> - 2012-01-20 11:46 -0500
Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-18 14:24 -0800
Re: macro jgk@panix.com (Joe keane) - 2012-01-18 22:36 +0000
Re: macro Kaz Kylheku <kaz@kylheku.com> - 2012-01-18 22:50 +0000
Re: macro jgk@panix.com (Joe keane) - 2012-01-17 22:08 +0000
Re: macro Keith Thompson <kst-u@mib.org> - 2012-01-15 08:00 -0800
Re: macro NEON China BLUE <chine.bleu@yahoo.com> - 2012-01-15 08:15 -0800
Re: macro Ian Collins <ian-news@hotmail.com> - 2012-01-16 09:04 +1300
Re: macro Philip Lantz <prl@canterey.us> - 2012-01-15 17:36 -0800
Re: macro Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-01-16 12:01 +0000
csiph-web