简体   繁体   English

序列化boost变体; 非常奇怪和迟钝的编译器错误(MSVC 2010)

[英]Serializing boost variant; very strange and obtuse compiler error (MSVC 2010)

I'm trying to serialize a class containing a boost variant for storage in an embedded MYSQL database. 我正在尝试序列化一个包含boost变体的类,以便在嵌入式MYSQL数据库中存储。 I was previously using a union but I converted it over to a variant because the boost serializer has out-of-the-box support for serialization of the variant type. 我以前使用的是union,但是我将它转换为变量,因为boost序列化程序对变量类型的序列化具有开箱即用的支持。

The issue is this compiler error: 问题是这个编译器错误:

4>default : error : 'which' out of range.");
4>                 ^
4>            detected during:
4>              instantiation of "Visitor::result_type boost::detail::variant::visitation_impl(int, int, Visitor &, VoidPtrCV, boost::mpl::false_, NoBackupFlag, Which *, step0 *) [with Which=boost::mpl::int_<0>, step0=boost::detail::variant::visitation_impl_step<boost::mpl::l_iter<boost::mpl::l_item<boost::mpl::long_<3L>, u32={unsigned int}, boost::mpl::l_item<boost::mpl::long_<2L>, i32={int}, boost::mpl::l_item<boost::mpl::long_<1L>, f32={float}, boost::mpl::l_end>>>>,
4>                        boost::mpl::l_iter<boost::mpl::l_end>>, Visitor=boost::detail::variant::destroyer, VoidPtrCV=void *, NoBackupFlag=boost::variant<u32={unsigned int}, i32={int}, f32={float}, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_,
4>                        boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_, boost::detail::variant::void_>::has_fallback_type_]" at line 1802 of "..\..\extern\boost/variant/variant.hpp"
4>              instantiation of "Visitor::result_type boost::variant<T0_, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16, T17, T18, T19>::internal_apply_visitor_impl(int, int, Visitor &, VoidPtrCV) [with T0_=u32={unsigned int}, T1=i32={int}, T2=f32={float}, T3=boost::detail::variant::void_, T4=boost::detail::variant::void_, T5=boost::detail::variant::void_, T6=boost::detail::variant::void_, T7=boost::detail::variant::void_, T8=boost::detail::variant::void_,
8>                                          ^
8>  

Here is the relevant code for the serialization of the variant: ... 以下是变体序列化的相关代码:...

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & m_apszValueNames;
        ar & m_apszEnumOptions;
        ar & m_asValues;
        ar & m_aValues;
        ar & m_Flags;
        ar & m_Type;
        ar & m_pszName;
        ar & sm_kpszValue1Name;
        ar & sm_kpszValue2Name;
        ar & sm_kpszValue3Name;
        ar & sm_kpszValue4Name;
    }

protected:

    static pcstr            sm_kpszValue1Name;
    static pcstr            sm_kpszValue2Name;
    static pcstr            sm_kpszValue3Name;
    static pcstr            sm_kpszValue4Name;

    pcstr                   m_pszName;

    u32                     m_Type;
    u32                     m_Flags;

    typedef boost::variant<u32, i32, f32> Value;

    Value                   m_aValues[ Values::Count ];
    std::string             m_asValues[ Values::Count ];

    const pcstr*            m_apszEnumOptions;

    pcstr                   m_apszValueNames[ Values::Count ];

};

Your help is very much appreciated. 非常感激你的帮助。

Your visitor is of result type void , and it effectively does nothing at all. 您的访问者的结果类型为void ,并且它实际上什么都不做。 When you do 当你这样做

ar & boost::apply_visitor( variant_visitor(), m_aValues );

you are trying to serialize a void value. 您正在尝试序列化void值。 If variant has serialization support out-of-the-box it should be enough to do 如果variant具有开箱即用的序列化支持,那么它就足够了

ar & m_aValues;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM