Boost logo

Boost :

From: Peder Holt (peder.holt_at_[hidden])
Date: 2007-03-01 15:09:50


2007/3/1, Matthias Schabel <boost_at_[hidden]>:
> > But wait, there is hope: you can do this:
> >
> > ////////
> >
> > template<class A>
> > struct ordinal {
> > void * tag() {
> > static int const tag_;
> > return &tag_;
> > }
> > template<class B>
> > bool operator<(ordinal<B> const& other) {
> > return std::less<void*>(tag(), other.tag());
> > }
> > };
> >
> > //////
> >
> > This solution should also be MT safe.
>
> Unfortunately, this doesn't function at compile-time.
> That is, what I really want to be able to do is:
>
> struct system1 : ordinal<...> { };
> struct system2 : ordinal<...> { };
> struct system3 : ordinal<...> { };
>
> typedef mpl::list<system2,system3,system1> list_type;
> typedef mpl::sort<list_type>::type sorted_list_type;
>
> where mpl::less<> can be defined for ordinal<...>
>
> It seems like a hard (maybe impossible) problem...

You could always use the solution we use in boost.typeof. When we
emulate typeof support for VC compilers, we use the following trick to
associate a unique integer with each type:

template<int N> struct encode_counter : encode_counter<N - 1> {};
template<> struct encode_counter<0> {};

//Need to default to a larger value than 4, as due to MSVC's ETI
errors. (sizeof(int)==4)
char (*encode_index(...))[5];

# define BOOST_TYPEOF_INDEX(T)
(sizeof(*boost::type_of::encode_index((boost::type_of::encode_counter<1005>*)0)))
# define BOOST_TYPEOF_NEXT_INDEX(next) friend char
(*encode_index(encode_counter<next>*))[next];

//Tie it all together
template<typename T=int>
struct ordinal
{
   //Get the next available compile time constants index
   BOOST_STATIC_CONSTANT(unsigned,value=BOOST_TYPEOF_INDEX(T));

   BOOST_STATIC_CONSTANT(unsigned,next=value+1);
   //Increment the compile time constant
   BOOST_TYPEOF_NEXT_INDEX(next);
};

It is not exactly a portable solution, though...

Peder

>
> Matthias
>
> ----------------------------------------------------------------
> Matthias Schabel
> 2859 Glen Oaks Drive
> Salt Lake City, UT 84109
> 801-706-5760 (cell)
> 801-484-0811 (home)
> matthias at stanfordalumni dot org
> ----------------------------------------------------------------
>
>
> 
>
>
>
>
> _______________________________________________
> 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