1 Mar 2007 01:12
Re: generating unique ordinal value at compile time
Lewis Hyatt <lhyatt <at> princeton.edu>
2007-03-01 00:12:32 GMT
2007-03-01 00:12:32 GMT
> 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();
(Continue reading)
RSS Feed