Search code examples
c++boostmsgpack

Using msgpack in C++ with variant data structures


In the question Deserializing a heterogeneous map with MessagePack in C++ an answer refers to a gist which contains an example of based on recursive boost::variant with msgpack. I am trying to replicate this with the latest msgpack-c library version (1.3.0) and getting a number of errors at compilation time. Some of these are related to changes in types in the API (msgpack types DOUBLE and RAW no longer present), others seem more fundamental:

In file included from msgpack-c/include/msgpack.hpp:10:0,
             from variant.cpp:2:
msgpack-c/include/msgpack/object.hpp: In instantiation of ‘const msgpack::v1::object& msgpack::v1::adaptor::convert<T, Enabler>::operator()(const
<... lengthy output snipped for clarity ...>
  msgpack::v1::object&, T&) const [with T = boost::detail::variant::void_>’ has no member named ‘msgpack_unpack’
 v.msgpack_unpack(o.convert());

and similar messages about no member named msgpack_pack.

Is there an updated gist or example of this that i compatible with the latest msgpack-c version?

I am compiling on a CentOS7 machine with gcc version 4.8.5, boost 1.58, msgpack-c included as header-only, using the following command:

g++ --std=c++11 -Imsgpack-c/include -o variant variant.cpp

Solution

  • Is there an updated gist or example of this that i compatible with the latest msgpack-c version?

    There are examples that use variant in the msgpack-c repository.

    See:

    https://github.com/msgpack/msgpack-c/blob/cpp_master/example/boost/msgpack_variant_capitalize.cpp https://github.com/msgpack/msgpack-c/blob/cpp_master/example/boost/msgpack_variant_mapbased.cpp

    msgpack-c contains variant adaptors now. It is based on the following idea:

    Deserializing a heterogeneous map with MessagePack in C++

    Here is the adaptor:

    https://github.com/msgpack/msgpack-c/blob/cpp_master/include/msgpack/v1/adaptor/boost/msgpack_variant.hpp

    The following discussion may help to understand the concept of variant:

    https://github.com/msgpack/msgpack-c/pull/349

    In order to use the adaptor, you need to define MSGPACK_USE_BOOST.

    See:

    https://github.com/msgpack/msgpack-c/wiki/v1_1_cpp_configure#msgpack_use_boost-since-120