Q
Qi
Let me show sample code to explain,
======================
Source file a.cpp (not header)
namespace {
class TestClass
{
int abc;
};
}
const std::type_info & getA()
{
return typeid(TestClass);
}
======================
Source file b.cpp (not header)
namespace {
class TestClass // same name as above class
{
// differ with another TestClass, so the type should be differ
long def;
};
}
const std::type_info & getB()
{
return typeid(TestClass);
}
======================
Tested in GCC 4.4.0 and 4.5.2, calling getA and getB, the
result is exactly same (== will return true).
So seems the RTTI for two TestClass are same.
I also tested in VC 2008, the typeid for both TestClass
are different, which is I expected.
However, seems previous VC also had that bug,
http://connect.microsoft.com/Visual...d-operator-on-instances-of-unnamed-class-type
Question:
Is it standard behavior, or undefined behavior, or a bug in GCC?
A further question:
Is it safe to use type_info to distinguish types?
i.e, different types (no matter in which namespace, which file),
will give different type_info?
The behavior in GCC really made me doubt that.
Thanks
======================
Source file a.cpp (not header)
namespace {
class TestClass
{
int abc;
};
}
const std::type_info & getA()
{
return typeid(TestClass);
}
======================
Source file b.cpp (not header)
namespace {
class TestClass // same name as above class
{
// differ with another TestClass, so the type should be differ
long def;
};
}
const std::type_info & getB()
{
return typeid(TestClass);
}
======================
Tested in GCC 4.4.0 and 4.5.2, calling getA and getB, the
result is exactly same (== will return true).
So seems the RTTI for two TestClass are same.
I also tested in VC 2008, the typeid for both TestClass
are different, which is I expected.
However, seems previous VC also had that bug,
http://connect.microsoft.com/Visual...d-operator-on-instances-of-unnamed-class-type
Question:
Is it standard behavior, or undefined behavior, or a bug in GCC?
A further question:
Is it safe to use type_info to distinguish types?
i.e, different types (no matter in which namespace, which file),
will give different type_info?
The behavior in GCC really made me doubt that.
Thanks