Talk:Run-time type information

Latest comment: 3 years ago by BernardoSulzbach in topic RTTI in C++ is only available for polymorphic types

Merging with datatype

edit
  • I vote to keep the entry separate. I believe that the theme allows for a more comprehensive text, and if it is merged, it would probably remain as it is.

I too oppose


Code example

edit

Provided code example won't compile with g++ (4.0.2). Compilation produces "source type is not polymorphic" error. Adding virtual destructor to the base class solves the problem.--Iwasz 21:59, 21 March 2006 (UTC)Reply

It compiles fine as of today with a recent g++ and clang++. Mlkj (talk) 22:34, 31 December 2015 (UTC)Reply

Only objects/classes?

edit

Currently, the article states:

  • In programming, RTTI [...] means keeping information about an object's data type in memory at runtime

Isn't it more correct to say:

  • In programming, RTTI [...] means keeping information about an entity's data type in memory at runtime

as runtime information can be kept for simple data types such as integers, too? --Abdull (talk) 12:04, 9 February 2008 (UTC)Reply

The variable article says variable has as synonyms object and identifier. Yet, I think there is the possibility of confusion with OOP objects. --Abdull (talk) 12:09, 9 February 2008 (UTC)Reply

Why is this identified as a C++ thing?

edit

Delphi has had RTTI since the very beginning, and it's integrated into the language and used throughout the runtime, unlike C++ where it was bolted on after the fact and is a special option that has to be enabled. —Preceding unsigned comment added by 69.46.35.146 (talk) 21:14, 22 April 2009 (UTC)Reply

I concur. Run-time type information (in the non-Proper Noun sense) is a feature that is available in all reflective languages, of which there are MANY. The term "Reflection" is virtually identical in meaning to RTTI but is much more accepted academically. Either this article should be merged with Reflection (computer programming), or it should document the other languages such as Delphi and D Programming Language that also refer specifically to "RTTI" as a proper noun, or perhaps this page should just be expunged as it is currently just a piece of technical information specific to C++, which makes it completely irrelevant to encyclopedia readers. --118.90.128.222 (talk) 11:53, 20 August 2009 (UTC)Reply

If RTTI is use also in other languages than C++, there should be parallel code examples wherever C++ code appears. Sae1962 (talk) 08:29, 26 January 2011 (UTC)Reply

The funny thing is that RTTI in other languages (Delphi, C#) is way better integrated and yet this article gives an impression that this is a C++ thing. — Preceding unsigned comment added by 84.43.121.62 (talk) 11:40, 9 October 2018 (UTC)Reply

The whole article describes RTTI as a C++ feature, then out of nowhere, for a single sentence it mentions Java cast besides dynamic_cast. This is very confusing for someone not familiar with the topic. --A Tejesember (talk) 17:56, 18 July 2019 (UTC)Reply

The article used to be more general in its wording. This was changed by an anonymous editor in 2008, I think (see diff). – Tea2min (talk) 10:53, 19 July 2019 (UTC)Reply

Ada also has run-time typing information: Ada Core OOP4.31.13.17 (talk) 18:41, 12 July 2021 (UTC)Reply

I will try to make this article more general. I think RTTI deserves an article, the issue is that, as it stands today, it's too focused on C++. BernardoSulzbach (talk) 16:38, 13 July 2021 (UTC)Reply

RTTI in C++ is only available for polymorphic types

edit

Run-time type information in C++ is only available for polymorphic types. Please see [expr.dynamic.cast] and [expr.typeid] in the C++20 standard. For example, an attempt to down-cast a pointer to an instance of a non-polymorphic class A down to a pointer to an instance of a non-polymorphic cass B derived from A is ill-formed:

   class A {};
   class B : public A {};
   
   int main()
   {
       A* a = new B;
       B* b = dynamic_cast<B*>(a); // ill-formed!
   }

An up-cast would compile and would work as expected, using only static information.

Similarly, [expr.typeid] says: "When typeid is applied to an expression other than a glvalue of a polymorphic class type, the result refers to a std::type_info object representing the static type of the expression." I.e., it does compile, but the result does not (and in fact cannot) represent the dynamic type of the expression. – Tea2min (talk) 19:03, 14 July 2021 (UTC)Reply

Thank you, and sorry for the undue revert. You are correct that actual RTTI will only be available for polymorphic types, regardless of typeid being defined or not. BernardoSulzbach (talk) 20:06, 14 July 2021 (UTC)Reply