vendredi 22 février 2019

Declaration works with auto but not by explicitly declaring the type?

I have the following class:

struct pool : public std::enable_shared_from_this<pool> {
     private:
      struct manager {
        explicit manager(const std::weak_ptr<pool> &pool) : m_pool{pool} {
        }
        explicit manager() = default;
        auto operator()(connection *conn) -> void;

       private:
        std::weak_ptr<pool> m_pool;
      };

     public:
      pool(const pool &) = delete;
      auto operator=(const pool &) -> pool & = delete;

      auto borrow() noexcept -> std::unique_ptr<connection, manager>;
}

where connection has the same visibility as pool.

In my tests I can use borrow() with auto:

auto p = std::make_shared<pool>();
auto conn = p->borrow();

but I can't declare a variable with the same type as the return type of borrow():

std::unique_ptr<connection, manager> conn;

with clang returning the error:

error: 'manager' is a private member of 'dbc::detail::pool'

Shouldn't these two be interchangeable?

Aucun commentaire:

Enregistrer un commentaire