Topic: Q: typeid on arrays & pointers


Author: James Kuyper <kuyper@wizard.net>
Date: 1998/11/25
Raw View
rado42@my-dejanews.com wrote:
>
> Hi all,
>
> I've a question:
>
>    char arr[10],
>        *ptr = 0;
>
>    const type_info & ta = typeid (arr),
>                    & tp = typeid (ptr);
>
>    bool same_types = (ta == tp);
>
> My compiler (MSVC) tells me that the types are the same.
>
> OTOH, arrays and pointers look to me, and actually are _somewhat_ different.
>
> Does anybody know what it should be (at least according to the standard)?

According to Section 5.2.8, p3: "When typeid is applied to an expression
other than an lvalue of a polymorphic class type, ... array-to-pointer
.... conversions are not applied to the expression."

> Q2:
> And BTW: why does the comparison operator of type_info (MSVC 6.0)
> return int and not bool? (MSVC complains a little about converting int to
> bool). Is it in the standard, and if so, is there any specific reason for it?

According to the standard, ==, != and type_info::before() all return
bool, not int.

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1998/11/25
Raw View
rado42@my-dejanews.com writes:

>I've a question:

>   char arr[10],
>       *ptr = 0;

>   const type_info & ta = typeid (arr),
>                   & tp = typeid (ptr);

>   bool same_types = (ta == tp);

>My compiler (MSVC) tells me that the types are the same.

The compiler is wrong. The array-to-pointer conversion is not
applied to the expression in the typeid. The requirements
on class type_info are that operator== returns true if and
only if the types are the same, which they are not.

If I had to guess, I'd guess the compiler is (incorrectly)
performing the array-to-pointer conversion.

References: 5.2.8 and 18.5.1 in the C++ standard.

--
Steve Clamage, stephen.clamage@sun.com

      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Biju Thomas <bijuthom@ibm.net>
Date: 1998/11/25
Raw View
rado42@my-dejanews.com wrote:
>
> I've a question:
>
>    char arr[10],
>        *ptr = 0;
>
>    const type_info & ta = typeid (arr),
>                    & tp = typeid (ptr);
>
>    bool same_types = (ta == tp);
>
> My compiler (MSVC) tells me that the types are the same.
>
> OTOH, arrays and pointers look to me, and actually are _somewhat_ different.
>
> Does anybody know what it should be (at least according to the standard)?

I think MSVC is wrong in this. The standard says that array-to-pointer
conversions should not be applied to expressions when applying typeid
operator. Here is the quote: (5.2.8/4)

"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. Lvalue   to   rvalue (4.1),
array   to   pointer (4.2), and function   to   pointer (4.3) conversions are
not applied to the expression."

> Q2:
> And BTW: why does the comparison operator of type_info (MSVC 6.0)
> return int and not bool? (MSVC complains a little about converting int to
> bool). Is it in the standard, and if so, is there any specific reason for it?
>

The operators '==' and '!=' are supposed to return bool, not int,
according to the standard. But, since there is direct promotion in
either way, this is no big deal, except for language lawyers :-)

Regards,
Biju Thomas
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: rado42@my-dejanews.com
Date: 1998/11/24
Raw View
Hi all,

I've a question:

   char arr[10],
       *ptr = 0;

   const type_info & ta = typeid (arr),
                   & tp = typeid (ptr);

   bool same_types = (ta == tp);

My compiler (MSVC) tells me that the types are the same.

OTOH, arrays and pointers look to me, and actually are _somewhat_ different.

Does anybody know what it should be (at least according to the standard)?

Q2:
And BTW: why does the comparison operator of type_info (MSVC 6.0)
return int and not bool? (MSVC complains a little about converting int to
bool). Is it in the standard, and if so, is there any specific reason for it?

Thanx for your help.

Regards
Rado

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]