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 4345516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:02:48+00:00 2026-05-21T12:02:48+00:00

I have written a function template for serialization of enums to/from our stream class

  • 0

I have written a function template for serialization of enums to/from our stream class (Yes, I know boost::serialization, but it is not an option in my situation). Enums by convention in our company are serialized as int:

template<typename T>
Stream& operator<<( Stream& s, T const& value )
{
    s << ( int ) value;
}

template<typename T>
Stream& operator>>( Stream& s, T & value )
{
    int v;
    s >> v;
    value = (T) v;
}

These are simple templates, and they work nicely also in my function templates for (de)serializing a vector of enumeration items. I’m worried though that they are overly generic, i.e. that they get applied also for types T that are not enums but can be cast to/from an int. Can I improve the enum-serialization templates (or maybe the vector-serialization templates) to make sure they only apply to vectors of enums?

  • 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-21T12:02:49+00:00Added an answer on May 21, 2026 at 12:02 pm

    There are two improvements to be made here: not always serializing as int (not all enums are), but as whatever the underlying type is. And, as your request, to only accept enums.

    The latter is easily solved with std::enable_if and std::is_enum:

    typename std::enable_if<std::is_enum<T>::value, Stream&>::type
        operator<<( Stream& s, T const& value )
    
    // and likewise for operator>>
    

    And for the former, do the following inside the function:

    Stream& operator<<( Stream& s, T const& value )
    {
        typedef typename std::underlying_type<T>::type safe_type;
        s << static_cast<safe_type>(value);
    }
    
    // and likewise for operator>>
    

    This requires C++0x.

    If that’s not an option, both enable_if and is_enum can be found within Boost. However, I think you’ll need to make underlying_type yourself. (And of course, in the worse case you can do all three yourself, though is_enum can be a pain, if I recall correctly.)

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

Sidebar

Related Questions

I have written a function that gets a given number of random records from
I have written the following code: #include <iostream> using namespace std; template <class T>
I have a simple function written in Oracle 9i (version 9.2.0.4.0) to simulate an
I have a function I've written that was initially supposed to take a string
For our online game, we have written tons of PHP classes and functions grouped
I have written an AIR Application that downloads videos and documents from a server.
I have written a site in Prototype but want to switch to jQuery. Any
I am using Symfony 1.3.2 on Ubuntu. I have written a little helper function
I have written some code in my VB.NET application to send an HTML e-mail
I have written a ruby script which opens up dlink admin page in firefox

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.