Boost logo

Boost :

From: Andrey Semashev (andysem_at_[hidden])
Date: 2007-03-04 11:30:56


Hello Matthias,

Thursday, March 1, 2007, 12:08:50 AM, you 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?

Just a thought... Maybe something like this might help:

typedef [implementation defined] uint_ptr_t;

template< typename >
void foo() {}

template< typename T, uint_ptr_t TagV = (uint_ptr_t)&foo< T > >
struct ordinal : public mpl::integral_c< uint_ptr_t, TagV >
{
};

struct system1 : ordinal< system1 > {};
struct system2 : ordinal< system2 > {};

The uint_ptr_t type should be large enough to accomodate a pointer to
function. It is needed since function pointers cannot be ordered.

I suspect, not all compilers would allow this.

-- 
Best regards,
 Andrey                            mailto:andysem_at_[hidden]

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