Boost logo

Boost Users :

Subject: Re: [Boost-users] boost/preprocessor checking if argument is a constant
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2010-10-08 20:35:47


On Fri, Oct 8, 2010 at 12:04 PM, Z S <baltcode_at_[hidden]> wrote:

> 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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net