
Hello, I have written this code to return 2 to the power x. #include <boost/preprocessor/repetition/for.hpp> #include <boost/preprocessor/seq/size.hpp> #include <boost/preprocessor/arithmetic/add.hpp> #include <boost/preprocessor/arithmetic/sub.hpp> #include <boost/preprocessor/arithmetic/mul.hpp> #include <boost/preprocessor/tuple/elem.hpp> #include <boost/preprocessor/seq/elem.hpp> #define PRED(r,state) BOOST_PP_TUPLE_ELEM(2, 1, state) #define OP(r,state) (BOOST_PP_MUL(BOOST_PP_TUPLE_ELEM(2, 0, state), 2), BOOST_PP_SUB(BOOST_PP_TUPLE_ELEM(2, 1, state), 1)) #define MACRO(r,state) (state) #define POWERSEQ(x) BOOST_PP_FOR((2,x), PRED, OP, MACRO) #define LAST_TUPLE(x) BOOST_PP_SEQ_ELEM(BOOST_PP_SUB(BOOST_PP_SEQ_SIZE(POWERSEQ(x)),1), POWERSEQ(x)) #define POWER2(x) BOOST_PP_TUPLE_ELEM(2, 0, LAST_TUPLE(x)) POWER2(6) I have to generate some sequence and take the last element of it and then extract the result. I didn't find such a macro already in PP. My code seems a bit too complicated. Is there a more trivial way to do this? rds,

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" <lists_ravi@lavabit.com> To: <boost-users@lists.boost.org> Sent: Wednesday, February 03, 2010 4:52 AM Subject: Re: [Boost-users] PP: 2 to the power x macro
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,

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? #define POWER2(a) (1<<a) // Example usage in the context of the preprocessor #if POWER2(3) == 8 #error "Success, correct calculation" #else #error "Failure" #endif Regards, Ravi

2010/2/4 Ravi <lists_ravi@lavabit.com>
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?
#define POWER2(a) (1<<a) // Example usage in the context of the preprocessor #if POWER2(3) == 8 #error "Success, correct calculation" #else #error "Failure" #endif
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 <hicham@mouline.org>
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