tags:

views:

105

answers:

2

I'm fully aware that the return value of std::type_info::name() is implementation-defined.

From the C++ standard (ISO/IEC 14882:2003 §18.5.1.7):

Returns: an implementation-defined NTBS.

My question is: why? Wouldn't this member function be much more useful if the standard dictated what the return value should be?

+2  A: 

Basically, if an implementation decides that they can't or doesn't want to support RTTI, they can just return "";. If the standard forced it to return something, they'd possibly kill any ability to have a compliant compiler for an environment where the resources for RTTI don't exist or want to be disabled (a microchip, for example.)

And let's not forget we don't want to force an ABI/name-mangling scheme on any compilers.

This follows the C++ philosophy "You don't pay for things you don't need."

GMan
+1 for the RTTI argument. But couldn't the standard dictate something like *return the unmangled name of the dynamic type if RTTI is supported and of the static type if not*?
Job
@Job: Possibly, but how does one format the type name? And where is the string stored? I agree it would be nice to have a consistent or at least dependable `name()` result, but unspecified is the best that can be done, given the nature of compilers. (How they handle types is their business, and we can't be sure how they're handling it and what the target platform is, and whether those allow a meaningful `name()` result.)
GMan
@GMan: I think `0` isn't a valid NTBS, so such an implementation should at least `return ""`.
sbi
@sbi: I think you're correct.
GMan
A bad argument, I'd say. The real philosophy is more like "You don't pay for what you don't _use_". A microchip compiler can just document that "std::type_info::name() is expensive (2KB) and should be avoided.". That is perfectly compliant. And a linker for such systems should have no problem discovering that std::type_info::name() is not called, and the associated string tables are unused as well.
MSalters
+2  A: 

Where we're talking about vendors returning different strings, I think it's just a "we do it this way, you change" "no, we do it this way, YOU change" thing between the compiler vendors. Even the Standards committee doesn't want to annoy the compiler teams, and creating some neutral, new standard that isn't employed by any vendor tends to mean finding something that's nonsensical anyway.

Why aren't they all the obvious namespaces::class::functions etc. already? Some current implementations may have historically found it convenient to have it match the linker-required mangled names, been paranoid (or had paranoid clients) re memory usage etc..

Tony