kakyo
kakyo

Reputation: 11580

The right way to include a zmq::socket_t member in a class?

With Visual Studio 2017, the following code does not compile due to deleted constructor


// MyClass.h

#include <zmq.hpp>

extern zmq::context_t g_context;

class MyCppZMQClass
{
public:
    MyCppZMQClass();
    ~MyCppZMQClass();

private:
    zmq::socket_t m_socket;
};

//  MyClass.cpp

#include "MyCppZMQClass.h"

zmq::context_t g_context = zmq::context_t(1);


MyCppZMQClass::MyCppZMQClass()
    :m_socket(g_context, zmq::socket_type::sub)
{
}


MyCppZMQClass::~MyCppZMQClass()
{
}

main cpp

// hello_cpp.cpp

#include "MyCppZMQClass.h"

int main()
{
    auto mc = MyCppZMQClass();
}

Compiler error:

1>g:\\hello_cpp\hello_cpp.cpp(73): error C2280: 'MyCppZMQClass::MyCppZMQClass(const MyCppZMQClass &)': attempting to reference a deleted function
1>g:\hello_cpp\mycppzmqclass.h(15): note: compiler has generated 'MyCppZMQClass::MyCppZMQClass' here
1>g:\hello_cpp\mycppzmqclass.h(15): note: 'MyCppZMQClass::MyCppZMQClass(const MyCppZMQClass &)': function was implicitly deleted because a data member invokes a deleted or inaccessible function 'zmq::socket_t::socket_t(const zmq::socket_t &)'
1>e:\_dev\vcpkg\installed\x64-windows\include\zmq.hpp(1513): note: 'zmq::socket_t::socket_t(const zmq::socket_t &)': function was explicitly deleted

I checked that I am calling this

// zmq.hpp

#ifdef ZMQ_CPP11
    socket_t(context_t &context_, socket_type type_)
        : socket_t(context_, static_cast<int>(type_))
    {
    }
#endif

and ZMQ_CPP11 is defined with VS2017

// zmq.hpp

#if (defined(__cplusplus) && __cplusplus >= 201103L) || (defined(_MSC_VER) && _MSC_VER >= 1900)
    #define ZMQ_CPP11
#endif

However, instantiating the socket object in a main function compiles.

int main()
{
    zmq::context_t ctx;
    zmq::socket_t sock(ctx, zmq::socket_type::push);
    sock.bind("inproc://test");
    std::string m = "Hello, world";
    sock.send(zmq::buffer(m), zmq::send_flags::dontwait);
}

What am I missing here?

Which constructor should I use in my former case?

UPDATE

If I delete my copy constructor and operator =, the problem persists.

#include <zmq.hpp>

extern zmq::context_t g_context;

class MyCppZMQClass
{
public:
    MyCppZMQClass();
    ~MyCppZMQClass();

    MyCppZMQClass(const MyCppZMQClass&) = delete;
    MyCppZMQClass& operator=(const MyCppZMQClass&) = delete;

private:
    zmq::socket_t m_socket;
};

Upvotes: 0

Views: 1449

Answers (2)

n. m. could be an AI
n. m. could be an AI

Reputation: 119847

auto mc = MyCppZMQClass();

Here you are trying to create a temporary and then use a copy constructor to initialise a variable from it. Change to

MyCppZMQClass mc;

or add a move constructor/assignment.

Upvotes: 1

yumetodo
yumetodo

Reputation: 1202

mark copy constructor and operator = as deleted

That is the correct way to resolve this problem.

MyCppZMQClass now doesn't request copy constructible or copy assignable.

Don't forget providing move ctor/assign op.

Upvotes: 1

Related Questions