www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - v0.95, typeid() not comparing correctly

reply Derek Parnell <derek psych.ward> writes:
The following code in 0.95 does not seem to use typeid values correctly
when used in "==" comparisions. This used to work in 0.94.


# import std.string;
# struct Foo{int a,b;};
# struct Bar{float a,b,c;};
# 
# void test( ... )
# {
#     for (int i = 0; i < _arguments.length; i++)
#     {
#         std.string.printf("arg=%d f=%d b=%d ",
#         _arguments[i], typeid(Foo), typeid(Bar));
# 
#         if (_arguments[i] == typeid(Bar))
#         {
#             std.string.printf("Bar\n");
#             _argptr += Foo.sizeof;
#         }
#         else if (_arguments[i] == typeid(Foo))
#         {
# 
#             std.string.printf("Foo\n");
#             _argptr += Bar.sizeof;
#         }
# 
#     };
# }
# 
# void main( )
# {
#   Foo f;
#   Bar b;
# 
#     test(f,b,f,b);
# }

I get ...

arg=4255952 f=4255952 b=4255968 Bar
arg=4255968 f=4255952 b=4255968 Bar
arg=4255952 f=4255952 b=4255968 Bar
arg=4255968 f=4255952 b=4255968 Bar

In other words, I'm passing a Foo, then a Bar, then a Foo then another Bar,
and the output says the right typeid() is in the argument, but the "=="
comparision doesn't pick it up.
 
-- 
Derek
Melbourne, Australia
16/Jul/04 5:05:41 PM
Jul 16 2004
parent reply Andy Friesen <andy ikagames.com> writes:
Derek Parnell wrote:

 The following code in 0.95 does not seem to use typeid values correctly
 when used in "==" comparisions.

You should be able to work around this by using === instead of ==. -- andy
Jul 16 2004
parent Derek <derek psyc.ward> writes:
On Fri, 16 Jul 2004 09:28:54 -0700, Andy Friesen wrote:

 Derek Parnell wrote:
 
 The following code in 0.95 does not seem to use typeid values correctly
 when used in "==" comparisions.

You should be able to work around this by using === instead of ==.

Thanks. I retested this with v0.94 and I was wrong. It didn't work with that version either. I then tried the example code in the docs and that failed too. However, I can confirm that "===" and "is" works. I guess I'll use that idiom unless told by Walter not to. -- Derek Melbourne, Australia
Jul 17 2004