multi line macro in multiline macro

Jan 30, 2018 at 6:13pm
hey is it possible to have multiline macros in muliline macros?
not sure how to do this, if anyone can help, here is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#define A(B)\
	int count = 0;\
	do\
	{\
		B = count++;\
	}\
	while(count < 10);

#define MyMacro(name, start, end) \
template<int START, int END> \
struct TEST \
{ \
	int a;\
}; \
A(end);\
	TEST<start, end> name##x;

MyMacro(GEO, 1, 2);



Error C2059 syntax error: 'do'
Error C2143 syntax error: missing ';' before '{'
Error C2447 '{': missing function header (old-style formal list?)
Error C2059 syntax error: 'while'




maybe there is an issue with the '\' which are needed for the macro
Last edited on Jan 30, 2018 at 6:15pm
Jan 30, 2018 at 6:44pm
You can't have a do-while loop outside of a function.
Jan 30, 2018 at 8:19pm
macros are not good practice in general.

that aside, this kind of macro is big trouble for specific reasons:

int main()
{
A(b); //ok
A(b); //multiple attempts to declare count in same scope generates confusing error msg
and the above error is exceedingly annoying to fix if you had a lot of code between the calls or worse, called it in a loop where the problem becomes masked.
}


I am not sure your template is correct either, but not 100% sure right this moment.

Macros like this cause all kinds of trouble. They give weird, unpredictable error messages that take a lot of time to work through, have type safety problems, and once working they can confuse debuggers and tools about where the actual problem really is. They are a lot like goto -- the language supports it, but its not really the best way to do most tasks.
Last edited on Jan 30, 2018 at 8:24pm
Jan 31, 2018 at 9:52am
oh man, i didn't even notice I'm trying to use a loop outside of a function :o
good point guys thanks :D
Topic archived. No new replies allowed.