|
Boost : |
From: jsiek_at_[hidden]
Date: 2000-06-23 15:53:21
I've uploaded conversion.zip which has the conversion class and a test
file. With a few workarounds the portability isn't too bad: works with
VC++, Intel, g++, and KCC but not Borland.
Cheers,
Jeremy
jsiek_at_[hidden] writes:
>
> Here's a suggestion for an addition to type traits (or wherever is
> appropriate).
>
> Last night I was browsing lambda library code and found this little
> gem. It is a traits class that tells you whether type A is convertible
> to another type B.
>
> conversion<A,B>::exists
>
> This is quite important for things like seeing if an iterator category
> tag derives from another tag, allowing class specialization (which
> can't be done with the usual function-argument based external
> polymorphism trick).
>
> The reason this shouldn't stay in the lambda library is that it is
> really more generally applicable... and I'd like to see it
> incorporated into boost sooner.
>
> One thing that needs to be looked into is how portable this is. It
> has only been tested with g++ and KCC (as far as I know). I'll do
> this.
>
> Cheers,
>
> Jeremy
>
>
>
>
>
> // -- conversion class --
> // by Andrei Alexandrescu
> // (slightly modified)
>
> // usage, for example: conversion<A*, B*>::exists = true if A inherits B
>
> template<class T> T dummy_create();
>
> template <class From, class To>
> class conversion
> {
> typedef char (&no)[1];
> typedef char (&yes)[2];
> static no check(...);
> static yes check(To);
> public:
> void foo();
> #if __KCC
> static From from;
> enum { exists = sizeof( check(from) ) == sizeof(yes) };
> enum { exists_twoway = conversion<From, To>::exists && conversion<To, From>::exists };
> enum { same_type = false };
> #else
> static const bool exists;
> static const bool exists_twoway;
> static const bool same_type = false;
> #endif
> };
>
> #ifndef __KCC
> template<class From, class To>
> const bool conversion<From,To>::exists = sizeof( check(dummy_create<From>()) ) == sizeof(yes);
>
> template<class From, class To>
> const bool conversion<From,To>::exists_twoway = conversion<From, To>::exists && conversion<To, From>::exists;
> #endif
>
> template <class T>
> class conversion<T, T>
> {
> public:
> static const bool exists = true;
> static const bool exists_twoway = true;
> static const bool same_type = true;
> };
>
>
> template <class From>
> class conversion<From, void>
> {
> public:
> static const bool exists = false;
> static const bool exists_twoway = false;
> static const bool same_type = false;
> };
>
> template <class To>
> class conversion<void, To>
> {
> public:
> static const bool exists = false;
> static const bool exists_twoway = false;
> static const bool same_type = false;
> };
>
> template <>
> class conversion<void, void>
> {
> public:
> static const bool exists = true;
> static const bool exists_twoway = true;
> static const bool same_type = true;
> };
>
> ------------------------------------------------------------------------
> IT Professionals: Match your unique skills with the best IT projects at
> http://click.egroups.com/1/3381/4/_/9351/_/961786659/
> ------------------------------------------------------------------------
>
>
>
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk