mercredi 27 mai 2015

Initializing ZMQ Message wants to use private constructor

I have written a C++11 program which uses ZMQ.

In one particular line I want to create a new message as a local variable and initialize it using the size of the vector serialized:

auto zm = zmq::message_t {serialized.size()};

This compiles fine on my machine (let's call it machine "A"; using Clang++ version 3.4.2-13), while on a colleague's machine ("B"; using Clang++ version 3.5.0-10) an error occurs:

error: calling a private constructor of class 'zmq::message_t'
    auto zm = zmq::message_t {serialized.size()};
              ^
/usr/include/zmq.hpp:192:9: note: declared private here
        message_t (const message_t&);
        ^

I do not know the exact versions of ZMQ on the two machines. When examining zmq.hpp on machines A and B, I find that they have the following differences that I consider relevant for this issue.

On machine A:

class message_t
{
    // ...

public:
    inline explicit message_t (size_t size_)
    {
        int rc = zmq_msg_init_size (&msg, size_);
        if (rc != 0)
            throw error_t ();
    }

    // ...

private:
    zmq_msg_t msg;
    message_t (const message_t&);
    void operator = (const message_t&);
};

On machine B:

class message_t : private zmq_msg_t
{
    // ...

public:
    inline message_t (size_t size_)
    {
        int rc = zmq_msg_init_size (this, size_);
        if (rc != 0)
            throw error_t ();
    }

    // ...

private:
    message_t (const message_t&);
    void operator = (const message_t&);
};

When comparing with the version of this file found at GitHub, I believe that machine A has the newer version.

When I change my code to this

zmq::message_t zmq {serialized.size()};

the error on machine B goes away.

My questions are:

  1. I intend to invoke the message_t (size_t) constructor. Why is there an attempt to use message_t (const message_t&), which is private, on machine B?

  2. The change in the code that makes the error go away removes the assignment operator. Why did the compiler then not complain about void operator = (const message_t&) being private, but message_t (const message_t&)?

  3. Why is there no error on machine A, although message_t (const message_t&) is also private there? I guess it has something to do with message_t (size_t) being declared explicit, but I do not understand the effects of this keyword.

Aucun commentaire:

Enregistrer un commentaire