preprocessor check if empty

P

Philipp Kraus

Hello,

I hope my question is not off-topic.
My problem is defined as

#define DOSOME(x, …) docppcall(x % __VA_ARGS__)

but if __VA_ARGS__ is empty the replace should be docppcall(x), without
the % char. The % char should be only set, if exists an argument.
How can I create a working solution?

Thanks

Phil
 
V

Victor Bazarov

I hope my question is not off-topic.
My problem is defined as

#define DOSOME(x, …) docppcall(x % __VA_ARGS__)

but if __VA_ARGS__ is empty the replace should be docppcall(x), without
the % char. The % char should be only set, if exists an argument.
How can I create a working solution?

So, the idea is to produce

DOSOME(a) ---> docppcall(a)
DOSOME(b, c, d) ---> docppcall(b % c, d)
DOSOME(6,4,3,2,1) --> docppcall(6 % 4, 3, 2, 1)

right? I think it's tricky, especially since 6 % 4 is a constant
expression, IIRC.

If you're not particularly in need of the first argument to docppcall to
be a const-expr, then you could create a class that would apply the mod
operator to the first two arguments, if there are more than one, or not
(if there is only one), and then forward the result and the other
(third, etc.) arguments to the 'docppcall' function, something like

template<class X> maybemod(X&& x) -> decltype(docppcall(x)) {
return docppcall(x); }

template<class X, class A1, class ... Types>
maybemod(X &&x, A1 &&a1, Types ... args
-> decltype(docppcall(x % a1, &args ...)) {
return docppcall(x % a1, &args ...); }

#define DOSOME(x, ...) maybemod(x __VA_ARGS__)

(I didn't verify that it works; it's only intended to give an idea).

V
 
P

Philipp Kraus

So, the idea is to produce

DOSOME(a) ---> docppcall(a)
DOSOME(b, c, d) ---> docppcall(b % c, d)
DOSOME(6,4,3,2,1) --> docppcall(6 % 4, 3, 2, 1)

right? I think it's tricky, especially since 6 % 4 is a constant
expression, IIRC.

If you're not particularly in need of the first argument to docppcall
to be a const-expr, then you could create a class that would apply the
mod operator to the first two arguments, if there are more than one, or
not (if there is only one), and then forward the result and the other
(third, etc.) arguments to the 'docppcall' function, something like

template<class X> maybemod(X&& x) -> decltype(docppcall(x)) {
return docppcall(x); }

template<class X, class A1, class ... Types>
maybemod(X &&x, A1 &&a1, Types ... args
-> decltype(docppcall(x % a1, &args ...)) {
return docppcall(x % a1, &args ...); }

#define DOSOME(x, ...) maybemod(x __VA_ARGS__)

(I didn't verify that it works; it's only intended to give an idea).


Thanks, I can describe it a bit more detailed.

I have got only this cases:
DOSOME(xxx) and DOSOME(yyy, zzz)

and I would like to get a macro-only solution. So the first call must
be docppcall("xxx") and in the second docppcall( yyy % zzz ).
I would like to have got one call, something like overload DOSOME(x)
and DOSOME(x,y)


Phil
 
V

Victor Bazarov

I hope my question is not off-topic.
My problem is defined as

#define DOSOME(x, …) docppcall(x % __VA_ARGS__)

but if __VA_ARGS__ is empty the replace should be docppcall(x), without
the % char. The % char should be only set, if exists an argument.
How can I create a working solution?
[...]

I have got only this cases:
DOSOME(xxx) and DOSOME(yyy, zzz)

and I would like to get a macro-only solution. So the first call must be
docppcall("xxx") and in the second docppcall( yyy % zzz ).
I would like to have got one call, something like overload DOSOME(x) and
DOSOME(x,y)

There is no overloading of macros, therefore since what you ask for is
not possible without overloading, you can't get it all with only the
preprocessor text substitution. Also, if you want to put the double
quotation marks on the single macro argument, and not on the first one
of the two-arg macro, it would be even trickier to do. I think you're
better off using two distinct macros.

V
 
Ö

Öö Tiib

Thanks, I can describe it a bit more detailed.

I have got only this cases:
DOSOME(xxx) and DOSOME(yyy, zzz)

and I would like to get a macro-only solution. So the first call must
be docppcall("xxx") and in the second docppcall( yyy % zzz ).
I would like to have got one call, something like overload DOSOME(x)
and DOSOME(x,y)

Interesting why you can't simply write DOSOME(yyy % zzz) and get rid
of the whole issue?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top