Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:18:11+00:00 2026-05-27T03:18:11+00:00

By default in Boost.Serialization, enum types are serialized as a 32-bit integer. But I

  • 0

By default in Boost.Serialization, enum types are serialized as a 32-bit integer. But I need to serialize some enum types as different width integer. I’ve tried to specialize the boost::serialization::serialize method, but it seems it doesn’t work for enums.

Here is my attempt:

#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/asio.hpp>

enum MyEnum_t
{
    HELLO, BYE
};

namespace boost 
{ 
namespace serialization
{

template< class Archive >
void save(Archive & ar, const MyEnum_t & t, unsigned int version)
{
    unsigned char c = (unsigned char) t;
    ar & c;
}

template< class Archive >
void load(Archive & ar, MyEnum_t & t, unsigned int version)
{
    unsigned char c;
    ar & c;
    t = (MyEnum_t) c;
}

} // namespace serialization
} // namespace boost

BOOST_SERIALIZATION_SPLIT_FREE(MyEnum_t)

int main(int argc, const char *argv[])
{
    boost::asio::streambuf buf;
    boost::archive::binary_oarchive pboa(buf); 

    buf.consume(buf.size()); // Ignore headers

    MyEnum_t me = HELLO;
    pboa << me;

    std::cout << buf.size() << std::endl; // buf.size() = 4, but I want 1

    return 0;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:18:12+00:00Added an answer on May 27, 2026 at 3:18 am

    This probably doesn’t work because enums aren’t a real type, I don’t think you can in general overload a function for a specific enum.

    You could accomplish what you want by doing the conversion to char in the serialization of whatever object contains your MyEnum_t. You could also do what Dan suggested and encapsulate the enum in a first-class type for which you can overload the serialization. Something like:

    class MyEnum_clone {
      unsigned char v_;
      MyEnum_clone(MyEnum_t v) : v_(v) {};
      operator MyEnum_t() const {return MyEnum_t(v_); };
    
      // serialization...
    };
    

    That still likely won’t be completetely transparent, though.

    However, I don’t see why you care about how the type is serialized. Isn’t the point of serializing that you don’t have to care about the internal representation of the serialization, as long as you can restore the object correctly. The internal representation seems like a property of the archive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use named back-references in Boost. I tried this, but it
My default choice for cross-platform IPC would be boost, but I saw it criticised
I need to serialize std::wstring by my own method. How to force boost to
Is the C++ Boost library usually included by default on most Linux distros?
I was looking at the boost serialization library, and the intrusive way to provide
I have a few questions about boost::regex: I tried an example below. 1) What
C++ by default provides a tree based map. With Boost you can get a
so I'm using the boost::serialization library, and I'm trying to overide how a class
for some quick testing of a serialization library I want to create a streambuf
The default syntax for Boost::Program_Options is --DEVICE iphone. How can I support syntax -DEVICE:iphone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.