简体   繁体   中英

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. 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.

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. When you do

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

you are trying to serialize a void value. If variant has serialization support out-of-the-box it should be enough to do

ar & m_aValues;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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