Jump to content
  • Advertisement
Sign in to follow this  
CTar

preprocessor (C++), is this correct?

This topic is 4601 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!