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