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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:48:34+00:00 2026-05-29T03:48:34+00:00

We have some code that looks like this: class Serializer { public: template<class Type>

  • 0

We have some code that looks like this:

class Serializer
{
public:
    template<class Type> void Write(const Type& value)
    {
        internal_write((byte*)value, sizeof(Type));
    }

    // some overloads of Write that take care of some tricky type we have defined

private:
    // implementation of internal_write
};

As one might guess this writes data to a disk. We have similar Read functions that more or less cast some bytes to a type. Not as robust as it could be, but it works, because we write on the same platform as we read–meaning that the bytes we write match what we need to read.

We are now moving to support multiple platforms. We have in several places code like:

unsigned long trust_me_this_will_be_fine = get_unsigned_long();
a_serializer.Write(trust_me_this_will_be_fine);

That works fine in the current world, but if we assume that one of the platforms we want to support has unsigned long as 32-bit and on another they are 64-bit, we are hosed.

I’d like to change Serializer::Write to only take explicitly-sized types as parameters. I thought about this:

class Serializer
{
public:
   void Write(uint32_t value) { ... }
   void Write(uint64_t value) { ... }
};

But I don’t think that really solves the problem because on the 32-bit system, an unsigned long will be automatically converted into a uint32_t but on a 64-bit system it will be auto-converted into a uint64_t.

What I really want here is to make Write(uint32_t) only accept parameters of type uint32_t–meaning that it would require an explicit cast. I don’t think there’s a direct way to do that–if I am wrong, please tell me.

Short of that, I can think of two ways to solve this.

  1. Declare (but don’t define) private versions of Serializer::Write for every type that could be auto-converted into a type we support.
  2. Don’t take a uint32_t directly, but a class that holds a uint32_t and only has an explicit constructor for a uint32_t.

Option 2 would look something like this:

class only_uint32
{
public:
    uint32_t _value;
    explicit only_uint32(uint_32 value) : _value(value) { }
};

class Serializer
{
public:
    void Write(only_uint32 value) { ... }
};

Then calling code looks like this:

unsigned long might_just_work = get_unsigned_long();
a_serializer.Write(static_cast<uint32_t>(might_just_work)); // should work, and be explicitly sized.
a_serializer.Write(might_just_work); // won't compile

I assume many people have solved this type of problem. Is there a preferred way of doing this that I haven’t thought of? Is one of my ideas terrible, great, workable, anything?

P.S.: Yes, I understand this is a super-long post. It’s a somewhat complex and detailed problem, though.

Update:

Thanks for the ideas and help. I think we’re going with solution that like this:

class Serializer
{
public:
    template<class Type> void Write32(const Type& value)
    {
        static_assert(sizeof(Type) == 4, "Write32 must be called on a 32-bit value.");
        internal_write(reinterpret_cast<byte*>(value), 4);
    }
    // overloads like Write64 and various tricky types as before.

private:
    // implementation of internal_write
};

This is a fairly low-cost (in terms of engineering time) solution that succeeds at pushing knowledge of what you’re actually saving out to the caller and forces the caller to know what they are calling.

  • 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-29T03:48:35+00:00Added an answer on May 29, 2026 at 3:48 am

    You could do something like this:

    template<typename T>
    class MyClass {
    public:
        static_assert(sizeof(T) == sizeof(uint32_t), "Invalid type");
        MyClass(T t) : data(t) { }
    
    private:
        uint32_t data;
    };
    

    Or if you want to accept only uint32_ts:

    template<typename T>
    class MyClass {
    public:
        static_assert(is_same<T, uint32_t>::value, "Invalid type");
        MyClass(T t) : data(t) { }
    
    private:
        uint32_t data;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like: static const std::string and( AND ); This
I have some code that looks like: class Parent { private Intermediate intermediateContainer; public
I have been reviewing some code that looks like this: class A; // defined
I have some code that looks like this: someFunc(value) { switch(value){ case 1: case
In VS 2005, I have some code that looks like this: ifs.open(foo); while (!ifs.eof())
I have some XML code that looks like this <SEARCHRESULTS> <FUNCTION name=BarGraph> <PARAMETER name=numList></PARAMETER>
Ok, I have some C# code that looks like this and I was wondering
I have some Objective-C code that looks like this: #define myVar 10 float f
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
I have some code in Netbeans 6.1 editor that looks like this fooString(8) fooString(8)

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.