https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110057

user202729 <user202729 at protonmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |user202729 at protonmail dot 
com

--- Comment #11 from user202729 <user202729 at protonmail dot com> ---
One way this could be implemented is the following.

* implement built-in `__builtin_vptr(object)` and `__builtin_vptr(Type)` that
obtains the pointer to the vtable (or NULL if the object type has no virtual
method)
* in the implementation `vector.pop_back()` etc., add the the following:

    if (__builtin_vptr(this->_M_impl._M_finish) != __builtin_vptr(_Tp))
      __builtin_unreachable();

* the optimizer should be able to figure out the rest.

What do you think about this approach?

--------

Alternatively, we can also use the existing `typeid()` --- although the
optimizer is not smart enough to optimize the following code yet.

```
#include<cstdio>
#include<typeinfo>
struct Base{
    Base() {}
    virtual void f(){ std::printf("Base\n"); }
};
struct Derived final: Base{
    Derived() {}
    virtual void f(){ std::printf("Derived\n"); }
};
void f(Base* b){
    if(!b or typeid(*b)!=typeid(Base)) __builtin_unreachable();
    b->f();
}
```

Reply via email to