macro definition for different compile configurations

  • Follow


Hi, 
 I'm stucked by something appears really simple at first sight.

I have a function definition with different parameter list in different compile configurations. for example.

--------------------------
CONF1:
int f(int x, int y){}
int g(int x){}

CONF2:
int f(int y){}
int g(){}
----------------------------
I'm trying to define some marco like

#if CONF1
#define PARAM(list) int x, list
#elseif CONF2
#define PARAM(list) list
#endif

But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.

It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.

thanks. 

tom
0
Reply freshthomas (139) 3/27/2012 7:29:11 AM

On 3/27/2012 3:29 AM, thomas wrote:
> Hi,
>   I'm stucked by something appears really simple at first sight.
>
> I have a function definition with different parameter list in different compile configurations. for example.
>
> --------------------------
> CONF1:
> int f(int x, int y){}
> int g(int x){}
>
> CONF2:
> int f(int y){}
> int g(){}

Why don't your functions declared returning a value return a value?  Are 
those supposed to be definitions or declarations?

You *do* know that in C++ you're allowed to have functions with the same 
name and different argument list, right?  That's called "overloading". 
You *can* declare/define your 'f' and 'g' all in the same scope.

> ----------------------------
> I'm trying to define some marco like
>
> #if CONF1
> #define PARAM(list) int x, list
> #elseif CONF2
> #define PARAM(list) list
> #endif

And use it HOW?  To achieve WHAT?

> But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.

"Cannot work" for what?

> It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.

What exactly are you *trying to do*?  You're stating that your solution 
is not working, but what is *the problem* that you're trying to solve? 
(And please don't tell us that the problem is that the solution is not 
working)

V
-- 
I do not respond to top-posted replies, please don't ask
0
Reply v.bazarov (788) 3/27/2012 11:58:08 AM


Ok...

#ifdef _A_
#define PARAM_WITH_COMMA int x,
#defien PARAM_NO_COMMA int x
#else
#define PARAM_WITH_COMMA
#define PARAM_NO_COMMA
#endif

And I have two versions of following functions
#ifdef _A_
int f(int x, int y);
int f(int x);
#else
int f(int y);
int f();
#endif

And I can write the two versions of the functions with the following declaration.
int f(PARAM_WITH_COMMA int y);
int f(PARAM_NO_COMMA);

In short I want to make my code neat and easy to read, that's to say, fewer "#ifdef" like stuff.

So I'm trying to write some macros to shorten some word usage.

I just gave an example above using two marco definitions.

Can I use just one macro to achieve the same thing?




On Tuesday, March 27, 2012 3:29:11 PM UTC+8, thomas wrote:
> Hi, 
>  I'm stucked by something appears really simple at first sight.
> 
> I have a function definition with different parameter list in different compile configurations. for example.
> 
> --------------------------
> CONF1:
> int f(int x, int y){}
> int g(int x){}
> 
> CONF2:
> int f(int y){}
> int g(){}
> ----------------------------
> I'm trying to define some marco like
> 
> #if CONF1
> #define PARAM(list) int x, list
> #elseif CONF2
> #define PARAM(list) list
> #endif
> 
> But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.
> 
> It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.
> 
> thanks. 
> 
> tom

0
Reply freshthomas (139) 3/27/2012 1:44:33 PM

On 2012-3-27 21:44, thomas wrote:
> Ok...

Please top quote. And keep your line width within 76
characters.

> And I can write the two versions of the functions with the following declaration.
> int f(PARAM_WITH_COMMA int y);
> int f(PARAM_NO_COMMA);
>
> In short I want to make my code neat and easy to read, that's to say, fewer "#ifdef" like stuff.
>
> So I'm trying to write some macros to shorten some word usage.

Using macro is neither neat nor easy to read.
Whenever you can avoid macros, just avoid them.

You are using C++. C++ can do most stuff that macro can do.
When you want to write a macro, rethink again.


> I just gave an example above using two marco definitions.
>
> Can I use just one macro to achieve the same thing?

There is comma_if in Boost library. You may check Boost
preprocessor library.


-- 
WQ
0
Reply no357 (668) 3/27/2012 1:53:57 PM

On Tuesday, March 27, 2012 3:29:11 PM UTC+8, thomas wrote:
> Hi, 
>  I'm stucked by something appears really simple at first sight.
> 
> I have a function definition with different parameter list in different compile configurations. for example.
> 
> --------------------------
> CONF1:
> int f(int x, int y){}
> int g(int x){}
> 
> CONF2:
> int f(int y){}
> int g(){}
> ----------------------------
> I'm trying to define some marco like
> 
> #if CONF1
> #define PARAM(list) int x, list
> #elseif CONF2
> #define PARAM(list) list
> #endif
> 
> But it cannot work for func g() definition in CONF1 since a "," is appended unexpectedly.
> 
> It seems to be really simple, but I am now a little dumb how to define one and solve it elegantly.
> 
> thanks. 
> 
> tom

Seems that you don't understand what I am talking about, just judging by some keyword mentioned. 
0
Reply freshthomas (139) 3/27/2012 2:05:35 PM

On Tue, 27 Mar 2012 07:05:35 -0700 (PDT), thomas
<freshthomas@gmail.com> wrote:

>On Tuesday, March 27, 2012 3:29:11 PM UTC+8, thomas wrote:
>> Hi, 
>>  I'm stucked by something appears really simple at first sight.
>> 
>
>Seems that you don't understand what I am talking about, just judging by some keyword mentioned. 

Try replying to the people your post is intended to reply to instead
of yourself.

You have not clearly stated your problem.

If you are coding C++ you do not need to write macros to define your
functions using compile time switches. You can simply define your
functions simultaneously in the same scope and let the compiler choose
which overloaded function to use based on the function call.

Defining:

int f(int x, int y){}
int f(int x){}
int f(){}

Calling:

i = f(x,y);
i = f(x);
i = f();

The compiler will choose the proper function that matches the
arguments in the function call.
0
Reply geoff745 (361) 3/27/2012 4:49:09 PM

Qi <no@no.com> wrote:
> C++ can do most stuff that macro can do.

  Not everything, though. For instance, there's a reason why assert()
is still a macro in C++. (Because it typically prints the file name
and line number where the assertion failed, plus the expression that
failed, that's basically impossible to achieve without a preprocessor
macro. Even if there was a way to get the line number otherwise (which
there isn't), you still couldn't stringify the expression without the
preprocessor.)
0
Reply nospam270 (2853) 3/27/2012 5:31:10 PM

On March, 27th, Geoff wrote:

[snip]

> Try replying to the people your post is intended to reply to instead
> of yourself.

[snip]

Gosh, it took me almost one minute to figure out what your sentence
actually means. I take it from your profile that you are a native
speaker, so I assume that such sentences are nothing strange. Still, I
wonder whether this is considered good English or not. A comma would
be a great hint for non-native speakers, but I guess that your
sentence is grammatically correct.

Regards,
Stuart
0
Reply DerTopper (387) 3/27/2012 6:27:14 PM

On Tue, 27 Mar 2012 11:27:14 -0700 (PDT), Stuart Redmann
<DerTopper@web.de> wrote:

>On March, 27th, Geoff wrote:
>
>[snip]
>
>> Try replying to the people your post is intended to reply to instead
>> of yourself.
>
>[snip]
>
>Gosh, it took me almost one minute to figure out what your sentence
>actually means. I take it from your profile that you are a native
>speaker, so I assume that such sentences are nothing strange. Still, I
>wonder whether this is considered good English or not. A comma would
>be a great hint for non-native speakers, but I guess that your
>sentence is grammatically correct.
>

Stuart,
I pumped it through LibreOffice just to see if it would flag the
grammar, it did not.

However, I suppose it could have been phrased better and more
formally:

"You should reply and quote the people to whom you are intending to
reply, rather than replying to yourself."

Better?
0
Reply geoff745 (361) 3/27/2012 6:46:52 PM

On 27 Mrz., 20:46, Geoff <ge...@invalid.invalid> wrote:
> On Tue, 27 Mar 2012 11:27:14 -0700 (PDT), Stuart Redmann
>
>
>
>
>
>
>
>
>
> <DerTop...@web.de> wrote:
> >On March, 27th, Geoff wrote:
>
> >[snip]
>
> >> Try replying to the people your post is intended to reply to instead
> >> of yourself.
>
> >[snip]
>
> >Gosh, it took me almost one minute to figure out what your sentence
> >actually means. I take it from your profile that you are a native
> >speaker, so I assume that such sentences are nothing strange. Still, I
> >wonder whether this is considered good English or not. A comma would
> >be a great hint for non-native speakers, but I guess that your
> >sentence is grammatically correct.
>
> Stuart,
> I pumped it through LibreOffice just to see if it would flag the
> grammar, it did not.
>
> However, I suppose it could have been phrased better and more
> formally:
>
> "You should reply and quote the people to whom you are intending to
> reply, rather than replying to yourself."
>
> Better?

Still looks weird to me.

"You should reply to the people to whom you are intending to reply,
rather than replying to yourself." looks somewhat better, even though
it may not be grammatically correct. Besides, it does not contain the
quoting part. Maybe like this:"You should reply to and quote the
people to whom you are intending to reply, rather than replying to
yourself."

Would that be OK?

Thanks,
Stuart
0
Reply DerTopper (387) 3/27/2012 7:39:39 PM

On 03/28/12 08:39 AM, Stuart Redmann wrote:
>
> "You should reply to the people to whom you are intending to reply,
> rather than replying to yourself." looks somewhat better, even though
> it may not be grammatically correct. Besides, it does not contain the
> quoting part. Maybe like this:"You should reply to and quote the
> people to whom you are intending to reply, rather than replying to
> yourself."
>
> Would that be OK?

Or simply direct the OP to

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4

-- 
Ian Collins
0
Reply ian-news (9878) 3/27/2012 7:49:22 PM

On 27 Mrz., Ian Collins wrote:

[snip]

> Or simply direct the OP to
>
> http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4

Acknowledged. However, I was intending to improve my English.



In that matter, I'd rather say
"You should reply to the people whom you are intending to reply to,
[...]"
than
"You should reply to the people to whom you are intending to reply,
[...]"

Would that still be correct?

Regards,
Stuart
0
Reply DerTopper (387) 3/27/2012 7:58:21 PM

On Tue, 27 Mar 2012 12:58:21 -0700 (PDT), Stuart Redmann
<DerTopper@web.de> wrote:

>On 27 Mrz., Ian Collins wrote:
>
>[snip]
>
>> Or simply direct the OP to
>>
>> http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4
>
>Acknowledged. However, I was intending to improve my English.
>
>
>
>In that matter, I'd rather say
>"You should reply to the people whom you are intending to reply to,
>[...]"
>than
>"You should reply to the people to whom you are intending to reply,
>[...]"
>
>Would that still be correct?
>
>Regards,
>Stuart

I have to say the latter is more correct. putting the 'to' after
'reply' is splitting the infinitive. The 'to' belongs to 'whom' and
not to the verb 'reply'. But it is also more formal. You were correct
when you called me out on the 'to reply to'. You should probably also
discard the 'to the people'.

"You should reply to whom you are intending to reply, rather than
replying to yourself."
0
Reply geoff745 (361) 3/27/2012 10:50:49 PM

Geoff <geoff@invalid.invalid> wrote:
> "You should reply and quote the people to whom you are intending to
> reply, rather than replying to yourself."

  "Rather than replying to yourself, you should reply to the people you
are intending to reply. It makes it clearer who you are talking to."
0
Reply nospam270 (2853) 3/28/2012 8:14:22 AM

13 Replies
50 Views

(page loaded in 0.148 seconds)

Similiar Articles:


















7/14/2012 6:34:08 AM


Reply: