Boost logo

Boost :

From: Lewis Hyatt (lhyatt_at_[hidden])
Date: 2007-02-28 19:12:32


> 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?

Hmm, I doubt you can do this with as much generality as you would like,
especially since it would be hard to ensure uniqueness across multiple
translation units.

What about a solution like this:

/////////////

struct ordinal_tag {};

template<class A>
struct ordinal {
        static ordinal_tag const tag;
        template<class B>
        bool operator<(ordinal<B> const& other) {
                return &tag < &other.tag;
        }
};

template<class A>
ordinal_tag const ordinal<A>::tag = ordinal_tag();

struct system1 : ordinal<system1> {
};

struct system2 : ordinal<system2> {
};

struct system3 : ordinal<system3> {
};

////////////////

This relies on being able to order pointers even when they don't point into the
same array, which is technically undefined behavior.

Now that I think about it, don't people use std::map<void*,T> all the time? How
do they make sure that is portable?

-Lewis


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