Consider following example:
#include <iostream>
#include <typeinfo>
int main()
{
int a=9;
std::cout << typeid(a).name() << '\n';
}
Output on g++ 4.8.1 : i
Output on MSVS 2010: int
Why output is compiler depedent? What is the reason behind this? Why it is left as implementation defined? Why not same output on all compilers? Does C++ standard says it explicitly?
Because compilers represent types differently and don't use the same internals.
The G++ string is the mangled type name, returning that (rather than demangling it) is less work and more efficient. Requiring compilers to demangle those strings would add more work. The standard leaves it up to implementors to decide if they want to do that.
If the standard dictated it then it would also have to specify all sorts of things, like whether to say signed long or just long and how to represent complex template instantiations that depend on other types and constants. The benefits of standardising those strings would be very small, but a large amount of work.
Yes, C++ standard says it explicitly:
18.7.1$9,10 Class type_info [type.info]
const char* name() const noexcept;
9 Returns: An implementation-defined ntbs.
10 Remarks: The message may be a null-terminated multibyte string (17.5.2.1.4.2), suitable for conversion and display as a wstring (21.3, 22.4.1.4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With