Diffrent typeid between platforms

446 views Asked by At

I don't understand why this programs produces differents outputs between Linux and Windows using the same compiler. In Windows it outputs float and in Linux it outputs f.

#include <typeinfo>
#include <iostream>

int main() {
        std::cout << typeid(float).name() << std::endl;
        return 0;
}
3

There are 3 answers

0
Cory Kramer On BEST ANSWER

The typeid operator returns the name from the std::type_info, which is described as

The class type_info holds implementation-specific information about a type, including the name of the type and means to compare two types for equality or collating order. This is the class returned by the typeid operator.

So it is implementation-specific how they choose to name the types. This is reinforced again in std::type_info::name

Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.

0
dfrib On

From [expr.typeid]/1:

The result of a typeid expression is an lvalue of static type const std​::​type_­info and dynamic type const std​::​type_­info or const name where name is an implementation-defined class publicly derived from std​::​type_­info which preserves the behavior described in [type.info]. [...]

the name is implementation-defined.

[type.info]/1 specifically mentions that [emphasis mine]:

The class type_­info describes type information generated by the implementation. Objects of this class effectively store a pointer to a name for the type, and an encoded value suitable for comparing two types for equality or collating order. The names, encoding rule, and collating sequence for types are all unspecified and may differ between programs.

the name for the type pointed to is unspecified and may vary not only between implementations but even between programs (from the same implementation).

0
Marek R On

There is cure for that. boost has demangling functionality.

#include <iostream>
#include <boost/core/demangle.hpp>
#include <typeinfo>


int main()
{
    auto name = typeid(float).name();
    std::cout << name << std::endl;
    std::cout << boost::core::demangle(name) << std::endl;
    
    return 0;
}

Simple demo

More complex demo