Sign in to follow this  

preprocessor (C++), is this correct?

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have had some problem with the preprocessor not correctly expanding a macro. I have tried to make the smallest code which shows the problem:
#include <boost/preprocessor/comparison/equal.hpp>
#include <boost/preprocessor/control/if.hpp>
#include <boost/preprocessor/arithmetic/inc.hpp>

#define A(n,l)	BOOST_PP_IF( BOOST_PP_EQUAL(n,l), int , B(n,l) )

#define B(n,l)	bool A(BOOST_PP_INC(n),l)

int main()
{
	A(0,1)
	
	return 0;
}

In Visual Studio 2005 and Dev-Cpp (4.9.9.2), the preprocessor output for main is:
int main()
{
	bool A(1,1)
	

	return 0;
}
I would have thought that A would have checked n (1) against l (1) and because they are equal it should have resulted in int, so the output should have been:
int main()
{
	bool int

	
	return 0;
}
Could anyone tell me what I have done wrong?

Share this post


Link to post
Share on other sites
The preprocessor is not recursive like that.

When it reach A(0, 1) it will expand to B(0,1), which in turn will expand to A(1,1).

A is not expanded here, because it has already been preprocessed

[edit]
That's why you can do constructs like this
#define printf(a) printf(a##%s, __LINE__)

(or something similiar, you get the idea. =)
[/edit]
[edit 2]
ouch, that construct was really bad. i wrote it in a hurry. =)
but anyway, it's not recursive...=)
[/edit 2]

Share this post


Link to post
Share on other sites
Thanks, I have heard about that before, but I didn't think it would be a problem since A never calls itself, it calls B which calls A.

Share this post


Link to post
Share on other sites

This topic is 4272 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this