I'm working with a codebase where I can't solve this circular dependency:
foo.h
class Foo
{
public:
using Ptr = std::shared_ptr<Foo>;
using ConstPtr = std::shared_ptr<const Foo>;
void setter(const Bar::Ptr& bar_ptr) {};
private:
Bar::WeakPtr bar_ptr_;
};
and
bar.h
class Bar
{
public:
using Ptr = std::shared_ptr<Bar>;
using ConstPtr = std::shared_ptr<const Bar>;
using WeakPtr = std::weak_ptr<Bar>;
Bar(Foo::ConstPtr foo_ptr) : foo_ptr_(std::move(foo_ptr)) {};
private:
Foo::ConstPtr foo_ptr_;
};
It was previously compiling because there was an external header where:
using FooPtr = std::shared_ptr<Foo>;
using FooConstPtr = std::shared_ptr<const Foo>;
using BarPtr = std::shared_ptr<Bar>;
But since consistency is sought after, I'd like to have Foo::Ptr, Foo::ConstPtr, Bar::Ptr. Any chance I can get it?
EDIT: added the Bar::WeakPtr which was previously missing since I thought I was already in trouble.
usingand not using a variable?usingis to hide the fact that it's astd::shared_ptrinstead of aboost::shared_ptrsometimes soon. It's actuallyusing Ptr = my_namespace::shared_ptr<Foo>;Foodoesn't have aBar::Ptrmember thatsetter()assigns to, in factsetter()doesn't participate in shared ownership ofBarat all, so it shouldn't be taking ashared_ptr<Bar>at all (herbsutter.com/2013/06/05/…), thus you could break the circular dependency by changingFoo::setter()tovoid setter(const Bar* bar_ptr)and then forward-declareBarFoohas aBar::WeakPtr. I simplified too much the example, let me fix it.