Hello,
I have written this code to return 2 to the power x.
#include
On Monday 01 February 2010 11:56:35 Hicham Mouline wrote:
I have written this code to return 2 to the power x.
Just a little confused reading your code; does POWER2(6) give you 64? Or are you trying to produce the sequence 1,2,4,8,16,32,64? If the former, why not simply use the left shift operator? Regards, Ravi
----- Original Message -----
From: "Ravi"
On Monday 01 February 2010 11:56:35 Hicham Mouline wrote:
I have written this code to return 2 to the power x.
Just a little confused reading your code; does POWER2(6) give you 64? Or are you trying to produce the sequence 1,2,4,8,16,32,64? If the former, why not simply use the left shift operator?
Regards, Ravi
I can't find this left shift operator in Boost.PP. Do you mean << ? But then how do I evaluate it in the context of the preprocessor? regards,
2010/2/4 Ravi
On Tuesday 02 February 2010 21:29:40 Hicham Mouline wrote:
I can't find this left shift operator in Boost.PP. Do you mean << ? But then how do I evaluate it in the context of the preprocessor?
Unless I have misunderstood your question (very likely), isn't this what you need?
How about this? // Must expand to "int foo8;", but it does not. int BOOST_PP_CAT(foo, POWER2(3)); Roman Perepelitsa.
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Roman Perepelitsa Sent: 04 February 2010 9:27 To: boost-users@lists.boost.org Subject: Re: [Boost-users] PP: 2 to the power x macro
How about this?
// Must expand to "int foo8;", but it does not. int BOOST_PP_CAT(foo, POWER2(3));
Roman Perepelitsa.
Exactly. Ravi, the macro expansion does not evaluate the << operator, that is done at compile-time. So Roman, what do you think of my POWER2 implementation? It is very slow (to preprocess) in an actual example I have. Can anyone see a faster implementation? Regards,
2010/2/4 Hicham Mouline
So Roman, what do you think of my POWER2 implementation?
It is very slow (to preprocess) in an actual example I have. Can anyone see a faster implementation?
I'd use something like this: #define POWER_2_0 1 #define POWER_2_1 2 #define POWER_2_2 4 #define POWER_2_3 8 #define POWER_2_4 16 #define POWER_2_5 32 #define POWER_2_6 64 #define POWER_2_7 128 #define POWER_2_8 256 #define POWER_2(n) BOOST_PP_CAT(POWER_2_, n) It's obviously fast and obviously correct. Roman Perepelitsa.
On Thursday 04 February 2010 10:49:25 Hicham Mouline wrote:
// Must expand to "int foo8;", but it does not. int BOOST_PP_CAT(foo, POWER2(3));
Exactly. Ravi, the macro expansion does not evaluate the << operator, that is done at compile-time.
I see now. Roman's proposed solution is the right one. If you look at any of the headers inside boost/preprocessor/arithmetic/, your solution (for the usual operators) and Roman's solution (for inc and dec) are really the only ones that are used. Regards, Ravi
participants (3)
-
Hicham Mouline
-
Ravi
-
Roman Perepelitsa