Boost logo

Boost :

From: Robert Allan Schwartz (notbob_at_[hidden])
Date: 2003-02-28 18:23:04


> > Perhaps my spelling class template could be folded into type_traits?
>
> [code snipped]
>
> Looks interesting, but I'm not sure if it's such a huge advantage over:
>
> template <typename T>
> void foo(T)
> {
> cout << "T is " << typeid(T).name() << endl;
> }
>
> The resulting string of your method is more portable of course, but is
> that the only reason?

No. There are other reasons why typeid() is not as "good" as my proposal:

1) You must #include <typeinfo> in order to use typeid(). This seems to me
to be unnecessary overhead.

2) The following program:

class base { };

class derived : public base { };

int main(void)
{
 base * b = new derived;

 base const * const b2 = new derived;

 foo(b);
 foo(*b);
 foo(b2);
 foo(*b2);

 return 0;
}

produces:

T is P4base
T is 4base
T is PC4base
T is 4base

when compiled by g++ and executed in cygwin.

As you point out, the string returned by typeid().name() is not specified by
the Standard, so it is not portable, but in this case, it is extremely
difficult to decipher.

I believe a "standardized" (within Boost), portable, and *readable* text
representation of T makes my proposal better than typeid().

Thanks for your feedback!

Robert


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