Thu, 11/24/2022 - 15:21
Hi everyone,
I came across the piece of code in opencascade::handle class that looks as follows:
template <class T2, typename = typename std::enable_if<is_base_but_not_same<T2, T>::value>::type>
operator const handle<T2>& () const
{
return reinterpret_cast<const handle<T2>&>(*this);
}
I believe this is undefined behavior. A small example that illustrates it:
class A : public Standard_Transient
{
public:
int a = 10;
};
class B : public A
{
};
void some_func(opencascade::handle<B> &b, const opencascade::handle<A> &a)
{
if(!a.IsNull())
{
b.Nullify();
std::cout << a->a << std::endl;
}
}
int main()
{
opencascade::handle<B> b(new B());
const opencascade::handle<A> &a = b;
some_func(b, a);
}
Take a look at some_func. Modifying `b` object shouldn't affect `a` according to aliasing rules.
Will be glad to hear the feedback, thanks