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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:12:09+00:00 2026-06-08T08:12:09+00:00

I am writing a sort of serialization class. It must provide functions for containers.

  • 0

I am writing a sort of serialization class. It must provide functions for containers. Current implementation is:

template <typename InsertIter>
bool readContainer(InsertIter result)
{
    typedef typename InsertIter::container_type::value_type tVal;
    UInt32 size = 0;

    if (!read(size))
        return false;

    for (UInt32 i = 0; i < size; ++i)
    {
        tVal val;
        if (!read(val))
            return false;
        *result++ = val;
    }
    return true;
}

template <typename InsertIter>
bool readMap(InsertIter result)
{
    typedef typename InsertIter::container_type::key_type tKey;
    typedef typename InsertIter::container_type::mapped_type tVal;
    UInt32 size = 0;

    if (!read(size))
        return false;

    for (UInt32 i = 0; i < size; ++i)
    {
        std::pair<tKey, tVal> pair;
        if (!read(pair))
            return false;
        *result++ = pair;
    }
    return true;
}

As you can see, I must to create different implementations for map-like types (std::map) and other containers, because std::map::value_type is std::pair(const K, V) and not std::pair(K, V).

So, I want to create method read(InsertIter) which will automatically select appropriate readContainer(InsertIter) or readMap(InsertIter). Is this possible?

  • 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-08T08:12:12+00:00Added an answer on June 8, 2026 at 8:12 am

    I have successfully solved my problem.

    Thanks to Johan Lundberg and especially to n.m. – I was not familiar with SFINAE idiom and your links and samples helped me a lot.

    I was not able to use C++11 features (project specifics), but they are not needed.

    Current code looks like this:

    struct SFINAE
    {
        typedef char __one;
        typedef struct { char __arr[2]; } __two;
    };
    
    template <typename T> 
    class has_mapped_type : public SFINAE
    {
        template <typename C> static __one test(typename C::mapped_type *);
        template <typename C> static __two test(...);
    public:
        enum { value = (sizeof(test<T>(0)) == sizeof(__one)) };
    };
    
    class Serializer
    {
        template <typename InsertIter>
        bool read(InsertIter result) const
        {
            return readContainerSelector< InsertIter, 
                has_mapped_type<typename InsertIter::container_type>::value
                > ::read(result, *this);
        }
    
        template <typename InsertIter, bool isMapType>
        struct readContainerSelector;
    
        template <typename InsertIter>
        struct readContainerSelector<InsertIter, true>
        {
            static bool read(InsertIter result, Serializer const& ser)
            {
                return ser.readMap(result);
            }
        };
    
        template <typename InsertIter>
        struct readContainerSelector<InsertIter, false>
        {
            static bool read(InsertIter result, Serializer const& ser)
            {
                return ser.readContainer(result);
            }
        };
    
        // methods from my topic post
        template <typename InsertIter> bool readContainer(InsertIter result);
        template <typename InsertIter> bool readMap(InsertIter result)
    
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scala programmer should have known that this sort of writing : class Person{ var
I'm writing some custom helpers the current one involves a sort of datagrid control
I am writing my own comparator class called PercentComparator and called the sort as
I am writing a custom domain-management class which uses various external services, sort of
I am writing a sort of image processing program that allows the user to
I am writing a counting sort function and when I run it, a window
So I am writing a Java code to represent a heap sort and to
Here's a sort of odd question. I'm in the process of writing a book
I'm beginning to learn perl and I'm writing a simple bubble sort using regular
I am writing an EXE wrapper (sort of packer) to protect my EXE and

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.