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

  • Home
  • SEARCH
  • 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 8818331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:05:00+00:00 2026-06-14T05:05:00+00:00

template <class ContainerType, class elementType> void SerializeContainer ( ContainerType< elementType > container ) {

  • 0
template <class ContainerType, class elementType>
void SerializeContainer (    ContainerType< elementType > container )
{
}

//call like this 
std::vector<int>  vector;
SerializeContainer(vector);

below will not compile.. Is there any way I can get through this?

  • 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-14T05:05:01+00:00Added an answer on June 14, 2026 at 5:05 am

    It is easier to use the value_type member of the container to extract the element type:

      template <typename T>
      void SerializeContainer (const T& container)
      {
          typedef typename T::value_type ElementType;
          ..........
      }
    

    The reason why your code does not work is because the syntax ContainerType<ElementType> only works when ContainerType is a really template. C++ support the template-template parameter for this:

    template <template <typename> class ContainerType, typename ElementType>
    void serializeContainer(const ContainerType<ElementType>& container) {
    }
    

    But even this will not match for standard C++ containers, because there are some hidden default arguments. When written explicitly:

    std::vector<int, 
                std::allocate<int> >   vector;
    

    So you’ll need to make the ContainerType parameter to accept 2 arguments:

    template <typename <typename, typename> C, typename E, typename A>
    void serializeContainer(const C<E, A>& container) {
    }
    

    But then this won’t match set::set because it has a different number of template parameters:

    std::set<int, 
             std::less<int>, 
             std::allocator<int> >  set;
    

    There simply isn’t a one-size-fit-all solution (before C++11) if you want the ElementType spelled explicitly when matching. It’s better to stick with the traits provided by the container.

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

Sidebar

Related Questions

I have a template class that looks something like this: template<class T> class C
So I have a template class that I would like to accept an std::map
This is what I have now: template<template<typename T> class container, typename T> inline bool
When using a container class like vector , list , etc., I can use
Okay, simple template question. Say I define my template class something like this: template<typename
I have a template class method like that: template<class T> static tmpClass<T>* MakeInstance(T value)
I have a template class defined like so: template <class T> class Command {
I've a basic template class, but I'd like to restrain the type of the
I am writing a template class which takes a floating-point-like type (float, double, decimal,
I have non-template class with a templatized constructor. This code compiles for me. But

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.