Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-09-26 10:00:06


I can't remember, does C++ allow int const[100][100] or even int const[100]
const[100] const (wacky stuff...)? Or does one have to make it: int const*
const* const stat = { ... }? It's been a very long time since I used an
array.

This is assuming one would want it constant since it is a compile-time
filled array. It is a shame that boost::array is only one-dimensional.

----- Original Message -----
From: Douglas Gregor
Newsgroups: gmane.comp.lib.boost.devel
Sent: Thursday, 2002:September:26 10:42
Subject: Re: compile time array initialization

On Thursday 26 September 2002 03:23 am, Gennadiy Rozental wrote:
> Let say I have one global array int stat[100][100]. And want to write an
> initializer for it that follow formula like this:
>
> if j2 > j >= j1 && i2 > i >= i1
> x[i][j] = i - i1;
> else
> x[i][j] = 0
>
> Or something similar.
>
> Obviously I could type all the elements, but it's inconvenient, difficult
> to change and error prone. Could MPL or BOOST_PP be of some help here?

The preprocessor library can do this. The result would look something like
this, but beware the untested code:

#define STAT_ELEMENT(J,I) \
  BOOST_PP_IF(BOOST_PP_AND(\
    BOOST_PP_AND(BOOST_PP_GREATER(J2,J),BOOST_PP_GREATER_EQUAL(J,J1)), \
    BOOST_PP_AND(BOOST_PP_GREATER(I2,I),BOOST_PP_GREATER_EQUAL(I,I2))),\
    I - I1, \
    0)

#define STAT_LINE(I,UNUSED) \
  { BOOST_PP_ENUM(100,STAT_ELEMENT,I) }

int stat[100][100] = {
  BOOST_PP_ENUM(100,STAT_LINE,BOOST_PP_EMPTY)
};

I didn't have any idea where I1, I2, J1, and J2 came from, so they are just
assumed to be macros. However, you could package them in a tuple if they
need
to be passed in.

Doug

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk