Date: Mon, 8 Apr 2024 06:47:05 +0100
On Sun, Apr 7, 2024 at 9:19 PM Jens Maurer wrote:
>
> struct Base { };
> struct Derived {};
I'm guessing you meant
struct Derived : Base {};
> int f(Base * b) { // not a template
> typeid(*b); // yields std::type_info object for "Derived"
> return 0;
> }
>
> int x = f(new Derived);
With regard to the member function std::type_info::is_polymorphic, the
best we can do is keep its behaviour consistent with how dynamic_cast
works. So for example if we have:
struct Base1 {};
struct Base2 { virtual ~Base2(void){} };
struct Base3 : Base2 {};
struct Derived : Base1, Base3 {};
Derived var;
Base1 * p1 = &var;
Base2 * p2 = &var;
Base3 * p3 = &var;
static_assert( false == typeid(*p1).is_polymorphic() );
static_assert( true == typeid(*p1).is_polymorphic() );
static_assert( true == typeid(*p1).is_polymorphic() );
That is to say: is_polymorphic() will return false when invoked on the
type_info of a pointer to a non-polymorphic base class (even if the
most derived object is polymorphic).
>
> struct Base { };
> struct Derived {};
I'm guessing you meant
struct Derived : Base {};
> int f(Base * b) { // not a template
> typeid(*b); // yields std::type_info object for "Derived"
> return 0;
> }
>
> int x = f(new Derived);
With regard to the member function std::type_info::is_polymorphic, the
best we can do is keep its behaviour consistent with how dynamic_cast
works. So for example if we have:
struct Base1 {};
struct Base2 { virtual ~Base2(void){} };
struct Base3 : Base2 {};
struct Derived : Base1, Base3 {};
Derived var;
Base1 * p1 = &var;
Base2 * p2 = &var;
Base3 * p3 = &var;
static_assert( false == typeid(*p1).is_polymorphic() );
static_assert( true == typeid(*p1).is_polymorphic() );
static_assert( true == typeid(*p1).is_polymorphic() );
That is to say: is_polymorphic() will return false when invoked on the
type_info of a pointer to a non-polymorphic base class (even if the
most derived object is polymorphic).
Received on 2024-04-08 05:47:18