Reputation: 52
I am working with c++ Macros, trying to implement on certain pattern. I have the following 3 macros:
#define First Objct t; t
#define Second a() / b()
#define Third ;
I've overload the lambda-ops in format: Objct [ Objct ]
So when I have in main() a line like:
First [ Second ] Third
it works fine, as it's 'translated' into:
Objct t; t[a()/b()]
(note: a()
and b()
are dummy functions returning type object.)
The hard part, is that I also have to make it work without the lambdas.
First Second Third
which means
Objct t; t a() / b()
In that case, I got a semicolon missing from FIRST
to SECOND
. I'm trying to figure out, what changes could be made (probably) to FIRST
macro so it can compile in both cases.
I'm not sure if I managed to explain myself properly. Any ideas?
Upvotes: 0
Views: 100
Reputation: 73456
Your question in pretty strange. I would strongly advise you not to use such a weird construct in real code. But I've looked at your question as if it were a funny puzzle.
Potential solution
I think there is no way to make both statement compile by changing only First
. But if you change Second
to:
#define Second +0,a() / b()
it compiles in both cases, under the sole condition that operator +
is defined for Objct
in combination with an int
. If you manage to implement this operator without side effect, it would even produce what you expect.
Why does it work ?
This definition makes use of the coma operator, the only issue being that the coma operator requires two expressions. +0 solves the issue syntactically, as +0
alone is valid, and t+0
is valid with the above-mentionned requirement.
With such a definition, First Second Third
is preprocessed as
Objct t; t +0,a() / b() ; // comma operator evaluates t+0
// then a()/b()
And First [Second] Third
would be preprocessed as
Objct t; t [+0,a() / b()] ; // comma operator makes +0 being evaluated
// and lost and [] is called with value of a()/b().
Upvotes: 1