Boost logo

Boost :

From: Brad King (brad.king_at_[hidden])
Date: 2002-06-12 15:33:27


> The variant implementation relies on the ability to map concrete types
> to integers. My implementation uses the index_of TypeList algorithm, for
> instance.
>
> So, unless we'll come-up with some birlliant way to do this mapping
> differently, we will not be able to implement a variant whose list of
> legal (concrete) types is 'open'.

Here is an idea that might work:

template <typename T>
class TypeId
{
public:
  static unsigned long GetId()
    { return reinterpret_cast<unsigned long>(&TypeId::Id); }
private:
  static void Id() {}
};

// ...

TypeId<int>::GetId();
TypeId<char>::GetId();

The linker (dynamic or otherwise) should ensure that there is only one
copy of TypeId<foo>::Id in any executable in memory (I'm not 100% sure
about this, though). This address will be unique, and will hopefully be
the same for the whole program. Comments on portability?

-Brad


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