如何将类模板参数传递给boost :: variant?

时间:2014-12-17 10:46:45

标签: c++ templates boost

我有一个使用boost :: get of boost:variant模块的模板方法:

typedef boost::variant<int, std::string, bool, uint8_t> Variant;

template <class T>
void write(const Variant& t) {
    size_t sizeT = boost::apply_visitor(SizeOfVisitor(), t);
    memcpy(&v[offset], &boost::get<T>(t), sizeT);
}

问题是我只在运行时知道Variant的基础类型。和AFAIK我只能用which()方法查询它。

有什么方法可以传递类型T,这是此方法的Variant的基础类型? 例如,使用which()我知道,有什么类型,但如何传递它?

switch (m_var.which()) { // Returns an int index of types in the order of passed template classes
    case 0: // This is int
    case 1: // This is std::string
    ...
}
...
Writer.write<???>(m_var); // How to pass the type here?

编辑:如果您知道任何其他方法来实现所需的结果 - 实际获取boost :: variant内部变量的地址,从那里复制,请与我分享您的想法

谢谢

2 个答案:

答案 0 :(得分:1)

我碰巧在这里写了一个非常相似的答案:

同样,最重要的是, 完全虚假 memcpy与非POD数据类型一起使用(所以 不能 std::string一起使用。永远。

使用仅在运行时知道的类型对variant进行操作的方法是使用boost::static_visitor<>

这里的main()示例适合接近您想要实现的目标,显然,

<强> Live On Coliru

#include <boost/variant.hpp>
#include <boost/bind.hpp>

#include <boost/array.hpp> // just as a sample
#include <iostream>

namespace serialization {

    namespace customization {
        template<typename T, typename Out, typename R = typename boost::enable_if<boost::is_pod<T>, void>::type>
            void do_serialize(T const& x, Out& out)
            {
                static_assert(boost::is_pod<T>(), "");
                char const* rawp = reinterpret_cast<char const*>(&x);
                std::copy(rawp, rawp+sizeof(T), out);
            }

        template<typename Out>
            void do_serialize(std::string const& x, Out& out)
            {
                do_serialize(x.size(), out);
                for(auto ch : x)
                    do_serialize(ch, out);
            }
    }

    struct serialize_f : public boost::static_visitor<> {
        template<typename Out, typename... T>
            void operator()(boost::variant<T...> const& v, Out& out) const
            {
                boost::apply_visitor(boost::bind(*this, _1, boost::ref(out)), v);
            }

        template<typename T, typename Out>
            void operator()(T const& x, Out& out) const
            {
                using customization::do_serialize; // ADL dispatch
                do_serialize(x, out);
            }
    };

    template <typename T, typename Out>
        Out serialize(T const& v, Out out) {
            const static serialize_f _vis {};
            _vis(v, out);
            return out;
        }

}

namespace MyUserTypes {

    struct A {
        std::string name;
        int i;
    };

    template<typename Out> void do_serialize(A const& v, Out& out) { // ADL will find this
        serialization::serialize(v.name, out);
        serialization::serialize(v.i, out);
    }
}

int main() {
    using namespace serialization;
    std::vector<uint8_t> binary_data;
    auto out_inserter = back_inserter(binary_data);

    // variants and custom types
    typedef boost::variant<MyUserTypes::A, boost::array<char, 42> > V;
    MyUserTypes::A myA { "0123456789", 99 };
    V v = boost::array<char,42>();

    serialize(myA, out_inserter);
    serialize(v, out_inserter);
    v = myA;
    serialize(v, out_inserter);

    std::cout << "Bytes in binary_data vector: " << binary_data.size() << "\n";
}

答案 1 :(得分:0)

switch (m_var.which()) { // Returns an int index of types in the order of passed template classes
    case 0: // This is int
      Writer.write<int>(m_var);
    break;
    case 1: // This is std::string
      Writer.write<std::string>(m_var);
    break;
    ...
}

:)