boost/preprocessor checking if argument is a constant
All the BOOST_PP functions only work on integer constants. I want to write macros, where the same interface can be used for both constants and variables, and only call the BOOST_PP if the argument is a constant, and use runtime code if it is a variable. For example, #define TWICE(n) BOOST_PP_IF( _IS_CONSTANT(n), \ BOOST_PP_MUL(n,2), \ ( (n) * 2) ) For more details, you can also see the original question here : http://stackoverflow.com/questions/3884489/mixing-variables-and-integer-cons... Is there a way to do so? The C preprocessor knows whether something is a constant, so we should be able to do this somehow. Or should I suggest this as a feature request for the Boost Preprocessor library? Thanks!
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I replied your question on stackoverflow. IMHO this is not a role for
cpp. Let the compiler take care of optimization here.
On Fri, 8 Oct 2010 12:04:30 -0400
Z S
All the BOOST_PP functions only work on integer constants. I want to write macros, where the same interface can be used for both constants and variables, and only call the BOOST_PP if the argument is a constant, and use runtime code if it is a variable. For example,
#define TWICE(n) BOOST_PP_IF( _IS_CONSTANT(n), \ BOOST_PP_MUL(n,2), \ ( (n) * 2) )
For more details, you can also see the original question here : http://stackoverflow.com/questions/3884489/mixing-variables-and-integer-cons...
Is there a way to do so? The C preprocessor knows whether something is a constant, so we should be able to do this somehow. Or should I suggest this as a feature request for the Boost Preprocessor library?
Thanks!
- -- Bryce Lelbach aka wash http://groups.google.com/group/ariel_devel -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkyvcw0ACgkQO/fqqIuE2t6ywgCcDa7FzHr/6lHZOzaetogrhTTC MzoAoO/MwDqzqgbwO2i0Rk8LUoEvMd7+ =0PgS -----END PGP SIGNATURE-----
On Fri, Oct 8, 2010 at 12:04 PM, Z S
All the BOOST_PP functions only work on integer constants. I want to write macros, where the same interface can be used for both constants and variables, and only call the BOOST_PP if the argument is a constant, and use runtime code if it is a variable. For example,
Is there a way to do so? The C preprocessor knows whether something is a constant, so we should be able to do this somehow. Or should I suggest this as a feature request for the Boost Preprocessor library?
Thanks!
I know you don't think this is always the case, but just as people have told you on stackoverflow, your compiler will already take your expression with integer literals and replace it with the appropriate value at compile-time. You are trying to optimize something through text preprocessing that has no reason to be. Such expressions that you describe are already required by the language to be used as compile-time constant values in other contexts, such as for array sizes, integral template parameters, etc. For instance some_template< 1 + 2 - 3 * 4 / 5 >, or some_array[ 6 * 7 % 8 ] have to be valid and both cases require those values to be compile-time constants (C99 VLAs not included). Don't assume that your compiler for some reason isn't capable of doing these computations at compile-time in your particular context, especially given that such expressions are already required by the language to be computable at compile-time -- you're looking for a solution to a problem that simply does not exist. Being pessimistic about what types of optimizations your compiler can and can't do is not a bad thing, but don't be foolish about it. -- -Matt Calabrese
On 10/8/2010 12:04 PM, Z S wrote:
All the BOOST_PP functions only work on integer constants.
This is false. Plenty of Boost PP functionality works with any preprocessor tokens, not just integer preprocessing tokens.
I want to write macros, where the same interface can be used for both constants and variables, and only call the BOOST_PP if the argument is a constant, and use runtime code if it is a variable. For example,
|#define TWICE(n) BOOST_PP_IF( _IS_CONSTANT(n), \
BOOST_PP_MUL(n,2), \
( (n)* 2) )
Do you mean by 'constant' a preprocessing token which is an integer ? If so, I think you are foolish to worry about whether BOOST_PP_MUL(n,2) or n * 2 is different since you are just generating some constant expression in either case, and at the very best you might be saving some incredibly small amount of compilation time one way or another.
participants (4)
-
Bryce Lelbach
-
Edward Diener
-
Matt Calabrese
-
Z S