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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:03:50+00:00 2026-06-16T00:03:50+00:00

I have a program which uses an unordered map with this definition: unordered_map<const char*,

  • 0

I have a program which uses an unordered map with this definition:

unordered_map<const char*, vector<pair<int, int> >, function<unsigned int (const char *str)> > myMap(30000, hashing);

where hashing is a function used for, well, hashing keys. My problem is -> how to serialize this kind of structure and deserialize it afterwards? I really need that, and to be fast and efficient as possible. I have been reading about boost libraries, but I don’t understand much how to use them with this kind of stuff. I know there is some boost library for unordered maps, but, obviously I’m doing something wrong with it and the compiler throws a lot of errors. Can someone give me an example code that works on how to serialize this? Thanks.

  • 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-06-16T00:03:50+00:00Added an answer on June 16, 2026 at 12:03 am

    TL;DR You cannot serialize std::function, use a regular predicate class instead.


    Serializing a map (whatever its kind) is easy. At heart it is:

    typedef std::map<std::string, std::string> Map;
    
    void serialize(Serializer& out, Map const& map) {
        out << map.size();
        for (auto const& p: map) { out << p.first << p.second; }
    }
    
    Map deserialize(Deserializer& in) {
        Map map;
    
        size_t size = 0;
        in >> size;
    
        for (size_t i = 0; i != size; ++i) {
            Map::key_type key;
            Map::mapped_type value;
            in >> key >> value;
            map[key] = value;
        }
    
        return map;
    }
    

    Except that in the general case both the comparator and allocator could be stateful right ?

    void serialize(Serializer& out, Map const& map) {
    +   out << map.value_compare() << map.get_allocator();
    
        out << map.size();
        for (auto const& p: map) { out << p.first << p.second; }
    }
    
    Map deserialize(Deserializer& in) {
    +   Map::key_compare comparator;
    +   Map::allocator_type allocator;
    +
    +   in >> comparator >> allocator;
    +
    +   Map map(comparator, allocator)
    
        size_t size = 0;
        in >> size;
        for (size_t i = 0; i != size; ++i) {
            Map::key_type key;
            Map::mapped_type value;
            in >> key >> value;
            map[key] = value;
        }
    
        return map;
    }
    

    So, in your case, assuming that Boost.Serialization knows how to handle std::allocator (it’s stateless), how is it supposed to serialize std::function<unsigned int(char const*)> ?

    Answer: it’s impossible because std::function performs type erasure and this clashes with C++ statically typed nature (and its lack of introspection). Once a type has been erased, it is not possible, in general, to recover it.

    You need the type of the comparator to be explicit in the type of the map OR to have an external mean to look it up. In any case, it may be hard to fit it into Boost.Serialization (context-less). You will probably be better up using a regular predicate class.

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

Sidebar

Related Questions

Currently, while compiling a C program which uses pthread library function I have to
I have a Haskell program which uses Data.Map.! at several places. After executing the
I have written a small program which uses function pointers to do some numerical
I have a multithreaded program which uses pthread mutexes for synchronization on few integers.
I have a C program which uses Lua for scripting. In order to keep
I have avery simple program which uses MediaPLayer. I am new to java and
Below, I have a simple program which uses the CImg library (http://cimg.sourceforge.net/) which iterates
I have a Java 3D program which uses lots (hundreds) of 3D models (3ds
I have a simple program (written in Java) which uses the google protocol buffer
I have been trying to create a simple program with Python which uses OpenCV

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.