July 20, 2004
So I finally did some digging into how typeid works, and solved my problems with implementing scanf just fine.  In hindsight it seems pretty obvious, but that's what I get for waiting this long to look into it.

Basically, typeid returns instances of TypeInfo objects and Phobos defines a set of TypeInfo classes for each primitive type.  The problem I had was that Phobos includes a pointer TypeInfo class which compares equal to any other pointer TypeInfo class.  However, the compiler generates a unique instance for each type, and this instance is returned for every typeid call on that type.  I think it might be useful to point this out in the documentation and I'd like to request that this implementation detail become a requirement.  As an example:

int main()
{
if( typeid( char* ) == typeid( int* ) )
printf( "same 1\n" );
if( typeid( char* ) is typeid( int* ) )
printf( "same 2\n" );
if( typeid( char* ) is typeid( char* ) )
printf( "same 3\n" );
return 0;
}

prints:

same 1
same 3

This is actually quite nice because I can both determine if something is a pointer type and determine what it's pointing to.


Sean


July 20, 2004
On Tue, 20 Jul 2004 01:48:07 +0000 (UTC), Sean Kelly wrote:

> So I finally did some digging into how typeid works, and solved my problems with implementing scanf just fine.  In hindsight it seems pretty obvious, but that's what I get for waiting this long to look into it.
> 
> Basically, typeid returns instances of TypeInfo objects and Phobos defines a set of TypeInfo classes for each primitive type.  The problem I had was that Phobos includes a pointer TypeInfo class which compares equal to any other pointer TypeInfo class.  However, the compiler generates a unique instance for each type, and this instance is returned for every typeid call on that type.  I think it might be useful to point this out in the documentation and I'd like to request that this implementation detail become a requirement.  As an example:
> 
> int main()
> {
> if( typeid( char* ) == typeid( int* ) )
> printf( "same 1\n" );
> if( typeid( char* ) is typeid( int* ) )
> printf( "same 2\n" );
> if( typeid( char* ) is typeid( char* ) )
> printf( "same 3\n" );
> return 0;
> }
> 
> prints:
> 
> same 1
> same 3
> 
> This is actually quite nice because I can both determine if something is a pointer type and determine what it's pointing to.
> 
> Sean

I found this stuff out too. In general, the typeid() of all classes is the
same, the typeid() of all structs is the same and the typeid() of al
pointers is the same. The curious thing is that the typeid() of any pointer
is the same as the typeid() of any struct. Thus you can't really tell if
you have a pointer to something or a struct.

<code>
class aClass{ int a;}
class bClass{ int b;}

struct aStruct{ int a;}
struct bStruct{ int b;}

void main()
{

    if( typeid( aClass ) == typeid( bClass ) )
        printf( "class = class\n" );
    if( typeid( aClass ) == typeid( aStruct ) )
        printf( "class = struct\n" );
    if( typeid( aClass ) == typeid( int* ) )
        printf( "class = pointer\n" );
    if( typeid( aClass ) == typeid( int[] ) )
        printf( "class = array\n" );
    if( typeid( aClass ) == typeid( int ) )
        printf( "class = native\n" );


    if( typeid( aStruct ) == typeid( Object ) )
        printf( "struct = class\n" );
    if( typeid( aStruct ) == typeid( bStruct ) )
        printf( "struct = struct\n" );
    if( typeid( aStruct ) == typeid( int* ) )
        printf( "struct = pointer\n" );
    if( typeid( aStruct ) == typeid( int[] ) )
        printf( "struct = array\n" );
    if( typeid( aStruct ) == typeid( int ) )
        printf( "struct = native\n" );


    if( typeid( int* ) == typeid( Object ) )
        printf( "pointer = class\n" );
    if( typeid( int* ) == typeid( aStruct ) )
        printf( "pointer = struct\n" );
    if( typeid( int* ) == typeid( char* ) )
        printf( "pointer = pointer\n" );
    if( typeid( int*) == typeid( int[] ) )
        printf( "pointer = array\n" );
    if( typeid( int* ) == typeid( int ) )
        printf( "pointer = native\n" );


    if( typeid( int[] ) == typeid( Object ) )
        printf( "array = class\n" );
    if( typeid( int[] ) == typeid( aStruct ) )
        printf( "array = struct\n" );
    if( typeid( int[] ) == typeid( int* ) )
        printf( "array = pointer\n" );
    if( typeid( int[]) == typeid( char[] ) )
        printf( "array = array\n" );
    if( typeid( int[] ) == typeid( int ) )
        printf( "array = native\n" );

    if( typeid( int ) == typeid( Object ) )
        printf( "native = class\n" );
    if( typeid( int ) == typeid( aStruct ) )
        printf( "native = struct\n" );
    if( typeid( int ) == typeid( int* ) )
        printf( "native = pointer\n" );
    if( typeid( int) == typeid( int[] ) )
        printf( "native = array\n" );
    if( typeid( int ) == typeid( char ) )
        printf( "native = native\n" );
}
</endcode>

-- 
Derek
Melbourne, Australia
20/Jul/04 1:33:21 PM