Visual C++ bug report

  • Thread starter Thread starter Marco Jez
  • Start date Start date
M

Marco Jez

I think I have found a bug in the Visual C++ compiler (version 7.1). What is
the preferred way for reporting it to Microsoft? Here's a brief description
of the suspect behavior: the expression typeid(A)==typeid(B) evaluates to
true when A and B are instances of two different unnamed classes, even
though it should evaluate to false. This only happens when A and B are
defined at namespace scope. Example:


#include <typeinfo>
#include <iostream>

class { int x; } A;
class { double x; } B;

int main()
{
std::cout << (typeid(A) == typeid(B)) << std::endl;
return 0;
}


The above code prints "1" to stdout while it should print "0" instead
because A and B are instances of two different types.

Cheers,
Marco
 
global scope is not required. they just have to be in the same namespace.
if you put them in namepace bla it has the same result.

std::cout << typeid(A).name() << std::endl;
std::cout << typeid(B).name() << std::endl;

you 'll see 2 times 'bla::__unnamed'

which is caused by the fact that your classes don't have class names.
the funny thing is that intellisense shows __unnamed_c609b043_1 for A and
__unnamed_c609b043_2 for B, so i'm surprised that they have the same
typeids.

that being said, i don't know if the behavior in this case is defined or
not.
the safest thing to do is to provide tag names.

kind regards,
Bruno.
 
Hi Bruno,
global scope is not required. they just have to be in the same namespace.
if you put them in namepace bla it has the same result.

I didn't say "global scope", I said "namespace scope"... :-)
that being said, i don't know if the behavior in this case is defined or
not.
the safest thing to do is to provide tag names.

The C++ Standard is pretty clear in explaining what you should expect from
the typeid operator, and unnamed classes are not mentioned as a particular
condition to be aware of. The paragraph that can be applied to my example is
the following:

"When typeid is applied to an expression other than an lvalue of a
polymorphic class type, the result refers to a type_info object representing
the static type of the expression. "

Unless I'm not misinterpreting that, the Standard states that typeid(A)
returns a type_info representing the static type of A, and typeid(B) returns
a type_info representing the static type of B. In my example the static type
of A is clearly different from that of B, but the two type_info objects
result to be equal (or at least indistinguishible one from the other), and
that's a violation of the above statement.

As for using tag names I agree with you, but I'm writing a
reflection/introspection framework, so being unable to distinguish between
two different types is a heavy deficiency for my project. I personally avoid
using unnamed classes, but the library I'm writing must exhibit consistent
behavior when used with foreign code I have no control on. :-/

Cheers,
Marco
 
Back
Top