Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Preprocessor possibilities

Reply
Thread Tools

Preprocessor possibilities

 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-13-2005
David Resnick <(E-Mail Removed)> wrote:

> I assume you reject the (clunky)


> #define TEST_1(type,val1) type array[]={val1}
> #define TEST_2(type,val1,val2) type array[]={val1,val2}


Yes

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-13-2005
Emmanuel Delahaye <(E-Mail Removed)> wrote:

> But I guess you want a variable list. Il suggest this :


(snip interesting technique)

> which is a trick I got here on c.l.c a few years ago. I'm using it day
> and night to automate code generation ! Very powerful ! (Perfect to give
> a string to an enum, for example...)


That does look like a creative technique, and I'll look for
opportunities to use it. Unfortunately, it doesn't handle the actual
situation I hav; I admit that what I want to do is not a stupendous
idea. The ultimate idea was to allow use of "array literals" (such as
are found in JavaScript) to allow for macros like

if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
/* ... */
}

None of the other options (inline function, C++ templates, etc.) are
particularly appetizing here either, so it seems that there is no
trick to avoiding tried-and-true (but boring) methods.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      12-13-2005
Robert Gamble <(E-Mail Removed)> wrote:

> #define TEST(x,y) x array[]={y}
> #define SEP ,
> TEST(int, 1 SEP 2 SEP 3);


> and I am not positive that even this is valid in C89.


It seems to work on my implementation, FWIW, but I concur that it is
probably too ugly to consider.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      12-13-2005
Christopher Benson-Manica <(E-Mail Removed)> writes:

> That does look like a creative technique, and I'll look for
> opportunities to use it. Unfortunately, it doesn't handle the actual
> situation I hav; I admit that what I want to do is not a stupendous
> idea. The ultimate idea was to allow use of "array literals" (such as
> are found in JavaScript) to allow for macros like
>
> if( TEST(int,1,(1,2,3)) ) { /* Checks for second argument in third */
> /* ... */
> }


For small ranges, you can do something like this:
#define BIT(x) (1ul << (x))
#define BITS(x, y) (BIT(x) | (y))

if (BIT(x) & BITS(1, BITS(2, BIT(3))))
Not ideal.
--
"For those who want to translate C to Pascal, it may be that a lobotomy
serves your needs better." --M. Ambuhl

"Here are the steps to create a C-to-Turbo-Pascal translator..." --H. Schildt
 
Reply With Quote
 
Jordan Abel
Guest
Posts: n/a
 
      12-13-2005
On 2005-12-13, Christopher Benson-Manica <(E-Mail Removed)> wrote:
> David Resnick <(E-Mail Removed)> wrote:
>
>> I assume you reject the (clunky)

>
>> #define TEST_1(type,val1) type array[]={val1}
>> #define TEST_2(type,val1,val2) type array[]={val1,val2}

>
> Yes


how about (c99 only)

#define TEST(type,...) type array[] = { __VA_ARGS__ }
 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      12-13-2005
Jordan Abel wrote:
> On 2005-12-13, Christopher Benson-Manica <(E-Mail Removed)> wrote:
> > David Resnick <(E-Mail Removed)> wrote:
> >
> >> I assume you reject the (clunky)

> >
> >> #define TEST_1(type,val1) type array[]={val1}
> >> #define TEST_2(type,val1,val2) type array[]={val1,val2}

> >
> > Yes

>
> how about (c99 only)
>
> #define TEST(type,...) type array[] = { __VA_ARGS__ }


Apparently you missed the part about c99 not being an option, but yes,
this would be the obvious solution if it were.

Robert Gamble

 
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
Compiler error occurred when try to use a flexible template expression in preprocessor definesCompiler error occurred when try to use a flexible template expression in preprocessor defines snnn C++ 6 03-14-2005 04:09 PM
Possibilities of setting up network? Phyllis Wireless Networking 1 09-03-2004 05:07 PM
preprocessor, token concatenation, no valid preprocessor token Cronus C++ 1 07-14-2004 11:10 PM
Looking for Suggestions on USB and other network possibilities Rob Computer Support 6 09-01-2003 10:37 AM
Forgot to finalize Audio CD-R: Solutions/possibilities? Antietam Computer Support 2 07-13-2003 03:00 AM



Advertisments