Re: preprocessor check if empty
On 9/21/2013 5:54 PM, Philipp Kraus wrote:
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
--
I do not respond to top-posted replies, please don't ask