Typeid() Returns Extra Characters in G++

typeid() returns extra characters in g++

Because it is a pointer to foo. And foo has 3 characters. So it becomes P3foo. The other one has type foo, so it becomes 3foo. Note that the text is implementation dependent, and in this case GCC just gives you the internal, mangled name.

Enter that mangled name into the program c++filt to get the unmangled name:

$ c++filt -t P3foo
foo*

Why does typeid.name() return weird characters using GCC and how to make it print unmangled names?

The return of name is implementation defined : an implementation is not even required to return different strings for different types.

What you get from g++ is a decorated name, that you can "demangle" using the c++filt command or __cxa_demangle.

C++ typeid.name() only returns char c

i want it recognize that char('9') is actually an int

You can use std::isdigit for this:

#include <cctype>

bool digit = std::isdigit(static_cast<unsigned char>(temp[i]);

How can I remove the number associated with typeid( ).name( ) in C++?

You probably shouldn't use type-codes and switch/downcasts for this. Instead, put your object-specific code into a virtual method or (depending on the problem at hand) use a visitor pattern. Your current approach will probably look something like this in the end:

int price = 0;

if (obj->get_type_str() == "armor")
{
price = 1000 + ((Armor *)obj)->get_durability() * 100;
}
else if (obj->get_type_str() == "ninjasword")
{
price = 5000 * ((NinjaSword *)obj)->get_damage();
}
else if (obj->get_type_str() == "bfg")
{
price = 20000;
}

shop->show_price(price);

The problem is, that this isn't very extensible. Instead of defining the price of an object along with the object, it will be littered around the code. A better way would be to provide a virtual method get_price(), that can do this:

class GameObject
{
public:
virtual ~GameObject() {}
virtual int get_price() const = 0;
};

class Armor: public GameObject
{
public:
virtual int get_price() const
{
return 1000 + durability * 100;
}

private:
int durability;
};

class NinjaSword: public GameObject
{
public:
virtual int get_price() const
{
return 5000 * damage;
}

private:
int damage;
};

class Bfg: public GameObject
{
public:
virtual int get_price() const
{
return 20000;
}
};

Now your object-specific code stays in the object and the previous code example simply becomes:

shop->show_price(obj->get_price());

If the code in question doesn't really belong to the object, this may be a case for the Visitor pattern.

Strange behaviour of the typeid operator?

What's going on is nothing special. Just that typeid doesn't promise to return the "original" name of the type, but just a name.

The function returns an implementation-defined string, which, if you're lucky, is recognizable, but it makes no promise of that.

About typeid function in C++

typeid function can get variable's type infomation in C++

It's true, but when u call the type_info::name() function you get an unique name for each different type, but the standard does not guarantee that the name is somewhat meaningful.
And it is also implementation-dependent.

What you get in practice (at least for most/all implementation) is the mangled name of that type.

In short, you can use type_info::name() only for comparison, or for debugging (and you'll have to demangle the name by yourself to get something meaningful)

EDIT
Since you use gcc, you may want to check out this page: http://gcc.gnu.org/onlinedocs/libstdc++/manual/ext_demangling.html

What does the typeid(var_name).name() function returns in C++?

typeid returns a std::type_info object and std::type_info::name returns const char* (a pointer). If this const char* points to an array containing one character and a NUL terminator, it will print the same way as single char.

The point is, you can't compare const char* with char. The function for comaring c-style string (with c-style string) is std::strcmp.

But... this is not a use-case for typeid. The type of the expression:

(-1 + sqrt(1 - 8 * t)) / 2)

is known at compile-time and does not change at runtime depending on its value. It will be always float. Note that you can check types (at compile time) with this:

std::is_integral<decltype((-1 + sqrt(1 - 8 * t)) / 2)>::value

The expression within decltype does not need to be evaluated (and it can't be) to get the type of an expression.

However, you essentially want this.

Strange output of std::typeid::name()

G++ uses implementation-defined naming for the types, but it also offers the utility c++filt to make them human-readable:

$ ./test | c++filt -t
unsigned long
A


Related Topics



Leave a reply



Submit