[zeromq-dev] [PATCH] zmq::msg_t::const_data for const correctness?

Martin Sustrik sustrik at 250bpm.com
Fri May 27 07:54:33 CEST 2011

Hi Steve,

> I'm sure there has been similar discussion before but it seems to be on
> other API, I propose a const_data() API to msg_t in order to permit
> const correctness in higher APIs.  A primary example being CZMQ's
> zmsgdup API (zmsg_copy).
> Is there a better way of doing this in C++?

This was already discussed on the list. Your patch is technically OK, 
but doesn't help much.

const void *data = msg.const_data ();

is equivalent to:

const void *data = msg.data ();

which can be done even today.

If you wanted to do strong type checking you would have use the 
following prototype:

const void *const_data () const;

Which means that users should use const message_t objects. That in turn 
means that coherent semantics of what "const message" is should be 
provided. Which turns out to be a problem as message_t object is just a 
pointer to reference counted buffer and C/C++ const semantics doesn't 
map well to reference-counted objects.

One way to avoid the problem is to hide the reference counted nature of 
message_t by providing CoW semantics (as std::string does). If you 
choose to do so, the API should be very clear on when the copying 
occurs. Implicit copying, especially with large messages would be a 
invisible & treacherous performance killer.


More information about the zeromq-dev mailing list