Boost logo

Boost :

From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-12-05 01:44:46


"Daryle Walker" <darylew_at_[hidden]> wrote in message
news:BAY23-DAV2FA8157E525BDF2DA9F2BBFB30_at_phx.gbl...
> On 12/3/04 4:59 PM, "Jonathan Turkanis" <technews_at_[hidden]> wrote:
>
> > Attached is some non-conforming code which automatically assigns a unique
> > integer id to an arbitrary type. It's based on this post by Daniel Wallin:
> >
> > http://lists.boost.org/MailArchives/boost/msg69842.php
> [TRUNCATE]
>
> I take that computing these IDs at compile-time is a requirement?

That's the idea. Fortunately it's not a requirement of anything, since it's not
really possible ;-)

> Otherwise
> you could use something like:
>
> reinterpret_cast<unsigned>(static_cast<void const *>(&typeid(T)));
>
> This assumes that a void-pointer can fit within an unsigned.

You don't need typeid -- you can just do:

   template<typename T>
   struct unique_id_holder {
       static char val;
   };

   template<typename T>
   char unique_id_holder::val;

   template<typename T>
   int unique_id()
   {
       return reinterpret_cast<int>(&unique_id_holder::val);
   }

Jonathan


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