Boost logo

Boost :

From: Arkadiy Vertleyb (vertleyb_at_[hidden])
Date: 2007-03-01 16:27:56


"Matthias Schabel" <boost_at_[hidden]> wrote

> For various reasons, it would be really nice to be able to generate
> a series of unique ordinal values (say integers, but really anything
> that is easily less_than_comparable) at compile time. A concrete
> example :
>
> struct system1 : ordinal<0> { };
> struct system2 : ordinal<1> { };
>
> etc...
>
> Now, if these tags can be defined in user code in various places,
> it can be difficult to ensure that there is no duplication of the
> ordinal
> values. What I want is something like this:
>
> struct system1 : ordinal<get_next_system_ordinal()> { };
> struct system2 : ordinal<get_next_system_ordinal()> { };
>
> where I don't really care what the specific order is, just that the
> ordinal values are unique... Anyone have a bright idea on how to
> accomplish this? Preprocessor?

Yes, the preprocessor can be used. VC++ has a __COUNTER__ extension. Also
there is BOOST_PP_COUNTER/BOOST_PP_UPDATE_COUNTER()(introduced in 1.34).

One problem -- if this used in different headers, the generated numbers
depend on the order of includes, which potentially can lead to the ODR
violation :-(

I once thought that the problem may be solved by compile-time GUID, but for
me four numbers instead of one didn't work... but if you want _anything_
less_than_comparable, this might be an option.

Regards,
Arkadiy


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