Boost logo

Boost :

From: Dirk Gerrits (dirk_at_[hidden])
Date: 2003-03-01 02:08:14


Robert Allan Schwartz wrote:
>>>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.

But wouldn't your method need an #include "spelling.hpp"?

> 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.

Ouch. That looks horrible indeed.

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

At what cost? The strings you generate are indeed very readable, but
specializing spelling for every type that might be a template parameter
someday seems like too much of a burden to me.

Others might disagree though...

Dirk Gerrits


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