0

I'm trying to investigate MsgPack's source code. In the example there's a fragment:

std::vector<std::string> vec;    
vec.push_back("MessagePack");
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, vec);

I see in /usr/include/msgpack/object.hpp that an Object to be packed must have method msgpack_pack:

template <typename Stream, typename T>
inline packer<Stream>& operator<< (packer<Stream>& o, const T& v)
{
    return detail::packer_serializer<Stream, T>::pack(o, v);
}

namespace detail {
template <typename Stream, typename T>
struct packer_serializer {
        static packer<Stream>& pack(packer<Stream>& o, const T& v) {
                v.msgpack_pack(o);
                return o;
        }
};
}

So I can't understand how compiler allows passing std::vector to msgpack::pack.

Andrey G.A
  • 305
  • 4
  • 20

1 Answers1

0

MessagePack includes built-in support for the STL.

As you can see src/msgpack/type/vector.hpp implements the << operator for a std::vector:

template <typename Stream, typename T>
inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<T>& v)
{
    o.pack_array(v.size());
    for(typename std::vector<T>::const_iterator it(v.begin()), it_end(v.end());
            it != it_end; ++it) {
        o.pack(*it);
    }
    return o;
}

And src/msgpack/type/string.hpp implements the same operator for a std::string:

template <typename Stream>
inline packer<Stream>& operator<< (packer<Stream>& o, const std::string& v)
{
    o.pack_raw(v.size());
    o.pack_raw_body(v.data(), v.size());
    return o;
}

Thus there is no problem to pack a std::vector<std::string>.

deltheil
  • 15,496
  • 2
  • 44
  • 64