C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Return value of type_info.name()

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Matthias Hofmann
Guest





PostPosted: Fri Aug 15, 2003 9:17 pm    Post subject: Return value of type_info.name() Reply with quote



Hello!

I am currently experimenting a bit with the typeid operator. I have found
out that under GCC 2.95 and 3.2 I get a different return value from
type_info.name() than under VC++ 6.0. Consider the following code:

class A {};

int main()
{
cout << typeid( A ).name() << endl;

cout << typeid( int ).name() << endl;

return 0;
}

Under GCC, the output I get is

1A
i

Under VC++ 6.0, the output I get is

A
int

Apart from the fact that I like the output from VC++ 6.0 better, I wonder
what the standard says about this - isn't type_info.name() supposed to
return the name of the type as it appears in my source code? That is, the
name for class A should be "A" and not "1A" (whereever that leading "1"
comes from), and the name of in int should be "int", not "i"?

Regards,

Matthias




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Matthias Hofmann
Guest





PostPosted: Sat Aug 16, 2003 1:30 pm    Post subject: Re: Return value of type_info.name() Reply with quote




Siemel Naran <SiemelNaran (AT) REMOVE (DOT) att.net> schrieb in im Newsbeitrag:
ISj%a.100843$3o3.7011991 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...
Quote:
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote in message

Apart from the fact that I like the output from VC++ 6.0 better, I
wonder
what the standard says about this - isn't type_info.name() supposed to
return the name of the type as it appears in my source code? That is,
the
name for class A should be "A" and not "1A" (whereever that leading "1"
comes from), and the name of in int should be "int", not "i"?

There's little restriction on what typeid can return. Except
typeid(A)!=typeid(B) should be true for A!=B where A and B are types.


That's a pity! I wonder why there are two functions raw_name() and name() if
both can return anything they like. The description of raw_name()'s return
value sounds like this function should be the one to return the "ugly" name,
the name used internally by the compiler - it seems obvious at first that
name() should return the class name as it appears in the code, but
unfortunately it doesn't...

Quote:
It would be nice if the name returned by typeid were standardized. It
would
include the namespace name if the class if not in the global namespace,
and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!


I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

Reagrds,

Matthias




[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Sat Aug 16, 2003 11:55 pm    Post subject: Re: Return value of type_info.name() Reply with quote



In article <bhks5f$d6e$1 (AT) news1 (DOT) nefonline.de>, Matthias Hofmann
<hofmann (AT) anvil-soft (DOT) com> writes
Quote:
It would be nice if the name returned by typeid were standardized. It
would
include the namespace name if the class if not in the global namespace,
and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!


I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

But, IIRC, typeid has to cope with much more than just fundamental and
class names. That means that specifying how compound types shall be
named interferes with the way an implementation represents such things
as mangled names.

--
Francis Glassborow ACCU
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
MJ
Guest





PostPosted: Sun Aug 17, 2003 12:04 am    Post subject: Re: Return value of type_info.name() Reply with quote

"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote

Quote:
Siemel Naran <SiemelNaran (AT) REMOVE (DOT) att.net> schrieb in im Newsbeitrag:
ISj%a.100843$3o3.7011991 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...
"Matthias Hofmann" <hofmann (AT) anvil-soft (DOT) com> wrote in message

Apart from the fact that I like the output from VC++ 6.0 better, I
wonder
what the standard says about this - isn't type_info.name() supposed to
return the name of the type as it appears in my source code? That is,
the
name for class A should be "A" and not "1A" (whereever that leading "1"
comes from), and the name of in int should be "int", not "i"?

There's little restriction on what typeid can return. Except
typeid(A)!=typeid(B) should be true for A!=B where A and B are types.


That's a pity! I wonder why there are two functions raw_name() and name() if
both can return anything they like. The description of raw_name()'s return
value sounds like this function should be the one to return the "ugly" name,
the name used internally by the compiler - it seems obvious at first that
name() should return the class name as it appears in the code, but
unfortunately it doesn't...

It would be nice if the name returned by typeid were standardized. It
would
include the namespace name if the class if not in the global namespace,
and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!


I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

The naming scheme convention is less obvious or intuitive when it
comes to anonymous classes.

E.g.:

class {
...
} x, y;

Borland: same name (name = "class") for both, x and y.
gcc: different names for x and y.

How would you standardize that?


Michael

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Sun Aug 17, 2003 12:05 am    Post subject: Re: Return value of type_info.name() Reply with quote

Matthias Hofmann wrote:
Quote:
Siemel Naran <SiemelNaran (AT) REMOVE (DOT) att.net> schrieb in im Newsbeitrag:
ISj%a.100843$3o3.7011991 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...

[snip]


Quote:
It would be nice if the name returned by typeid were standardized.
It would include the namespace name if the class if not in the
global namespace, and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!



I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

But it could be a problem for some users, who don't want their
executables made larger to store information they don't need.
There are many uses of C++ in environments where this much
additional memory isn't available.

There are additional complications in generating unique names
for local classes too. They're probably not insurmountable, and
might need to be tackled in any case if/when local classes get
linkage to allow them to be used as template parameters.

Also note that compilers might be able to better than a naive
standardised approach would be able to mandate: would you prefer
to see
std::basic_string<char, std::char_traits
or
std::string
? I'd prefer the latter, but this would require the compiler to
track which typedef code had used, because there could be many
typedefs for the same type. Maybe a perfect compiler would have
configuration settings to control whether it returned the real
type name, the "best" typedef simplification, or all available
information.

-- James.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Andrei Alexandrescu
Guest





PostPosted: Tue Aug 19, 2003 12:35 am    Post subject: Re: Return value of type_info.name() Reply with quote

"MJ" <c141592653589 (AT) hotmail (DOT) com> wrote

Quote:
The naming scheme convention is less obvious or intuitive when it
comes to anonymous classes.

E.g.:

class {
...
} x, y;

Borland: same name (name = "class") for both, x and y.
gcc: different names for x and y.

How would you standardize that?

Very simple. Named classes with linkage have as name() the fully-qualified
name as it would be accessed from the global namespace, e.g. A::B::Shape.

Unnamed types and types defined inside functions have name() implementation
dependent, while still unique across a program.


Andrei



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
MJ
Guest





PostPosted: Tue Aug 19, 2003 10:37 am    Post subject: Re: Return value of type_info.name() Reply with quote

James Dennett <jdennett (AT) acm (DOT) org> wrote

Quote:
Matthias Hofmann wrote:
Siemel Naran <SiemelNaran (AT) REMOVE (DOT) att.net> schrieb in im Newsbeitrag:
ISj%a.100843$3o3.7011991 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...

[snip]


It would be nice if the name returned by typeid were standardized.
It would include the namespace name if the class if not in the
global namespace, and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!

I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

[...]

Also note that compilers might be able to better than a naive
standardised approach would be able to mandate: would you prefer
to see
std::basic_string<char, std::char_traits or
std::string
? I'd prefer the latter, but this would require the compiler to
track which typedef code had used, because there could be many
typedefs for the same type. Maybe a perfect compiler would have
configuration settings to control whether it returned the real
type name, the "best" typedef simplification, or all available
information.

I would prefer a solution that doesn't depend on configuration settings.
Different types of "name()" could be offered:

type_info.name()
type_info.verbose_name()
type_info.compact_name()
...


Michael

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
James Dennett
Guest





PostPosted: Sat Aug 23, 2003 2:43 pm    Post subject: Re: Return value of type_info.name() Reply with quote

MJ wrote:

Quote:
James Dennett <jdennett (AT) acm (DOT) org> wrote

Matthias Hofmann wrote:
Siemel Naran <SiemelNaran (AT) REMOVE (DOT) att.net> schrieb in im Newsbeitrag:
ISj%a.100843$3o3.7011991 (AT) bgtnsc05-news (DOT) ops.worldnet.att.net...

[snip]

It would be nice if the name returned by typeid were standardized.
It would include the namespace name if the class if not in the
global namespace, and
the function name if it is a local class. Many compilers (like MSVC and
EDG) come close to this. But for portable code don't rely on it!

I wonder why this was not standardized. This should be no problem for
compiler writers to implement.

[...]

Also note that compilers might be able to better than a naive
standardised approach would be able to mandate: would you prefer
to see
std::basic_string<char, std::char_traits or
std::string
? I'd prefer the latter, but this would require the compiler to
track which typedef code had used, because there could be many
typedefs for the same type. Maybe a perfect compiler would have
configuration settings to control whether it returned the real
type name, the "best" typedef simplification, or all available
information.

I would prefer a solution that doesn't depend on configuration settings.
Different types of "name()" could be offered:

type_info.name()
type_info.verbose_name()
type_info.compact_name()
...

Then there is additional cost both in
(1) specifying sufficiently many forms to satisfy all users
(2) implementing all of these forms
(3) teaching/learning about them
(4) space overhead in implementations to support them.

Would you also want to have type_info::mangled_name(), to
help with dynamic loading on systems that use name mangling
to get type-safe linkage? Any other *_name() forms? I don't
think this will fly.

-- James.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.