I have a base and derived class:
struct Base
{
std::vector<Bar> bars;
std::unique_ptr<Fruit> fruit_ptr;
}
struct Derived : public Base
{
Foo foo;
}
And a template class joining an int and another object:
template<typename T>
struct WithCounter
{
int counter = 0;
T payload;
}
Is there any way I can allow and implement casting WithCounter<Derived>*
to WithCounter<Base>*
?
I know both types are unrelated, and that this can at least break if Derived
has a more strict alignment than Base
. If I ensure at compile time that alignment is the same, is there any world where I can allow this?
This problem is part of answering whether intrusive smart pointers can be implemented by a template class while keeping the cast feature of shared_ptr, and at the benefit of being single pointer based, for a better potential of lock-free atomic operations, in a constrained development environment (whose actual constraints are out of topic here)