Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Macro improperly terminated macro invocation

In C++ is there a mechanism to allow non terminated macros to be expressed? This is a contrived example:

#define MACRO(x, y) x + y
#define MACROC1(x) MACRO(x, 
#define MACROC2(y) y)
//...expecting 3
int foo = MACROC1(1) MACROC2(2);

I receive the error improperly terminated macro invocation from MSVC.

When I run cl -E file.cpp I see that the code below has been generated:

int c = 1 + 1 +  2);

In visual studio compilation fails with errors: error C2059: syntax error : ')' IntelliSense: improperly terminated macro invocation

like image 423
MW_dev Avatar asked Apr 09 '26 08:04

MW_dev


2 Answers

I don't think this is possible. The C precompiler expands macros depth-first, so the MACROC1 will be full expanded before the MACROC2 is even considered. Then, it will find the MACRO with and incomplete argument list and throws an error.

Generally speaking, you should avoid defining macros that build other macro calls. Compilers tend not to agree in what those mean.

like image 59
rodrigo Avatar answered Apr 11 '26 20:04

rodrigo


Your code would translate to :

int foo = MACRO(1, 2;

Which is wrong - it is an incomplete (improperly terminated) invocation of macro MACRO.

like image 37
littleadv Avatar answered Apr 11 '26 21:04

littleadv



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!