Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Preprocessor - can it make conditional decisions

Reply
Thread Tools

Preprocessor - can it make conditional decisions

 
 
David Thompson
Guest
Posts: n/a
 
      08-26-2007
On Sat, 11 Aug 2007 09:18:27 -0500, "Stephen Sprunk"
<(E-Mail Removed)> wrote:

> "Army1987" <(E-Mail Removed)> wrote in message
> news(E-Mail Removed)...
> > On Fri, 10 Aug 2007 19:18:55 -0500, Stephen Sprunk wrote:
> >> #define PUT_BYTE(const_index, val) \
> >> do { \
> >> if (!const_index) { *(int*)77 = (val); } \
> >> else { *(int *)99 = (val); } \
> >> } while (0)

(versus)
> > #define PUT_BYTE(const_index, val) \
> > ( *(int *)((const_index) ? 99 : 77) = (val) )

>
> It didn't occur to me to use the trinary operator on the left side of an
> assignment expression; that's probably an improvement, though I consider it
> less readable and you should end up with the same code generated.
>

Aside: it's officially the 'conditional' operator and commonly called
'ternary', but I've never heard it called 'trinary'.

> > (Note that it always evaluates each argument exactly once.)

>
> It's better in that respect.
>

No, they're the same. The if-else version _evaluates_ only one
occurrence of (val), although it's _written_ twice -- and thus
expanded twice, but expansion by itself isn't a problem, only
execution of those expanded instances.

I personally like the ?: version as it better captures the semantic
similarity, and perhaps even better something like:
* (int*) (int[]){77,99} [idx] = val;
or more legibly
* (int*) addrs [idx] = val
where addrs is arranged to be in scope (most simply global)
as something like static const int addrs [] = {77,99};

But the if-else version is definitely valid -- if storing through
those pointer values is, which applies to all approaches.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Decisions, Decisions.... Max Burke NZ Computing 25 09-24-2006 05:58 AM
Can html perform interactively or make decisions? senhortomas@hotmail.com HTML 15 02-28-2006 05:09 PM
Decisions, decisions... Waterspider Digital Photography 8 12-28-2005 09:48 PM
Decisions Decisions Larry Digital Photography 4 06-28-2005 03:41 PM
Scripts for helping you to make decisions rdsteph@mac.com Javascript 0 03-28-2005 03:43 PM



Advertisments