最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Casting between pointer to aggregate classes with a common member and another deriving member - Stack Overflow

programmeradmin5浏览0评论

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)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论