C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Extend std::type_info with more information

From: Jens Maurer <jens.maurer_at_[hidden]>
Date: Sun, 7 Apr 2024 22:19:40 +0200
On 07/04/2024 19.59, Frederick Virchanza Gotham via Std-Proposals wrote:
>
> On Sunday, April 7, 2024, Jens Maurer wrote:
>
>
> How would that allow to determine the type of a
> polymorphic object, where only a pointer-to-base
> might be given?
>
>
>
> Not sure what you're asking here. Let's say we have:
>
> class Base { . . . };
> class Derived : public Base { . . . };
>
> There are three possibilities here:
> (1) Neither Base nor Derived are polymorphic
> (2) Both Base and Derived are polymorphic
> (3) Only Derived is polymorphic
>
> Specifically, which of these 3 scenarios are you asking about? Could you give a few lines of sample code?

[expr.typeid] p3

"When typeid is applied to a glvalue whose type is a polymorphic class type (11.7.3), the result refers to a
std::type_info object representing the type of the most derived object (6.7.2) (that is, the dynamic type)
to which the glvalue refers."

struct Base { };
struct Derived {};

int f(Base * b) { // not a template
  typeid(*b); // yields std::type_info object for "Derived"
  return 0;
}

int x = f(new Derived);


Jens

Received on 2024-04-07 20:19:45