Subject: Re: [Boost-bugs] [Boost C++ Libraries] #9843: binary serializer wrong behaviour treating enums
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2014-04-10 07:09:22
#9843: binary serializer wrong behaviour treating enums
-------------------------------+-----------------------------------------
Reporter: jpjps@⦠| Owner: ramey
Type: Bugs | Status: reopened
Milestone: To Be Determined | Component: serialization
Version: Boost 1.55.0 | Severity: Problem
Resolution: | Keywords: enum seriliazation typesize
-------------------------------+-----------------------------------------
Changes (by jpjps@â¦):
* status: closed => reopened
* resolution: invalid =>
Comment:
simple snippet:
{{{
enum class SyncCode : unsigned char
{
ProcessId,
ModuleFileName,
};
std::ostringstream stream;
binary_oarchive archive(stream, boost::archive::no_header);
archive << SyncCode::ProcessId;
auto firstSize = stream.str().size();
stream = std::ostringstream();
archive << (unsigned char)SyncCode::ProcessId;
auto secondarySize = stream.str().size();
}}}
firstSize results 4, secondarySize 1.
on vs2013 / windows8.1 x64, targeted x86.
simple trick works on this case:
{{{
//oserializer.hpp
template<class Archive>
struct save_enum_type
{
template<unsigned char TypeSize>
struct same_sized_integral_type; // undefined
template<>
struct same_sized_integral_type<1>
{
typedef unsigned char type;
};
template<>
struct same_sized_integral_type<2>
{
typedef unsigned short type;
};
template<>
struct same_sized_integral_type<4>
{
typedef unsigned int type;
};
template<>
struct same_sized_integral_type<8>
{
typedef unsigned long long type;
};
template<class T>
static void invoke(Archive &ar, const T &t){
// convert enum to integers on save
const same_sized_integral_type<sizeof(T)>::type i =
static_cast<same_sized_integral_type<sizeof(T)>::type>(t);
ar << boost::serialization::make_nvp(NULL, i);
}
};
}}}
-- Ticket URL: <https://svn.boost.org/trac/boost/ticket/9843#comment:2> Boost C++ Libraries <http://www.boost.org/> Boost provides free peer-reviewed portable C++ source libraries.
This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:16 UTC