Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant construct object with move constructor using boost object_pool

I am trying to create a object using boost object_pool, but trying to use the move constructor of the desired object, but on Visual 2013, I am always getting:

error C2280: 'MyObject::MyObject(const MyObject &)' : attempting to reference a deleted function

The error happens because boost pool construct method always assume a const parameter.

Sample code:

#include <boost/pool/object_pool.hpp>

class MyObject
{
    public:
        MyObject():
            m_iData(0)
        {

        }

        MyObject(MyObject &&other):
            m_iData(std::move(other.m_iData))
        {           
            other.m_iData = 0;
        }

        MyObject(const MyObject &rhs) = delete;

        MyObject &operator=(const MyObject &rhs) = delete;

    private:
        int m_iData;
};

int main(int, char**)
{
    boost::object_pool<MyObject> pool;

    MyObject obj;

    MyObject *pObj = pool.construct(std::move(obj));
}

Is there any way to invoke the move constructor using boost::object_pool?

Thanks

like image 648
bcsanches Avatar asked Jun 06 '26 18:06

bcsanches


2 Answers

No it isn't supported in the present version of boost. In order to support what you require the boost pool will have to provide the following signature for construction:

template <typename T0> element_type * construct(T0 && a0) { ... }

and since this will be a templated function where both lvalues and rvalues can bind to the argument, the implementation will have to correctly dispatch the construction for both types of values.

You can find the available construction signatures in boost/pool/detail/pool_construct.ipp.

like image 108
mockinterface Avatar answered Jun 08 '26 10:06

mockinterface


The error message indicates that you are trying to convert a function to an rvalue-reference.

The line MyObject obj(); declares a function called obj, it is not valid to do std::move on a function.

I guess you meant MyObject obj;

like image 23
M.M Avatar answered Jun 08 '26 10:06

M.M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!