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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:15:09+00:00 2026-06-13T00:15:09+00:00

I have an advanced C++ question: Suppose I have a mmap_allocator template class, which

  • 0

I have an advanced C++ question: Suppose I have a mmap_allocator
template class, which is a subclass of std::allocator template
class and a mmappable_vector template class which is a subclass
of std::vector template class:

    template <typename T>
    class mmap_allocator: public std::allocator<T> {
            ...
    };

    template <typename T, typename A = mmap_allocator<T> >
    class mmappable_vector: public std::vector<T, A> {
            ...
    };

What I can do is convert from a mmappable_vector (with an mmap_allocator)
to a std::vector (with the standard allocator) using a function template:

    template <typename T>
    std::vector<T> to_std_vector(const mmappable_vector<T> &v)
    {
            return std::vector<T>(v.begin(), v.end());
    }

but the other way seems not to be possible:

    template <typename T>
    mmappable_vector<T> to_mmappable_vector(const std::vector<T> &v)
    {
            return mmappable_vector<T>(v.begin(), v.end());
    }

The problem when defining a constructor like:

    typedef typename std::vector<T, A>::iterator iterator;

    mmappable_vector(iterator from, iterator to):
                    std::vector<T,A>(from, to)
    {
    }

this uses iterators with the mmap_allocator and hence does not match
the call in to_mmappable_vector. On the other hand defining a
constructor:

    mmappable_vector(std::vector<T,std::allocator<T> > v):
            std::vector<T,std::allocator<T> >(v)
    {
    }

fails because the

    std::vector<T,std::allocator<T> > 

is not a base class of the mmappable vector.

How do I write a function template that converts std::vectors to
mmappable_vectors? Is this possible at all within C++?

Thanks for any insights,

  • Johannes
  • 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-13T00:15:10+00:00Added an answer on June 13, 2026 at 12:15 am

    You do not have template constructor in your mmappable_vector which takes two iterators of any type. Like this one:

    template <typename T, typename A = mmap_allocator<T> >
        class mmappable_vector: public std::vector<T, A> {
          typedef std::vector<T, A> Base;
          ...
    
          template <typename Iter>
          mmappable_vector(Iter first, Iter last, A a = A()) : Base(begin, end, a) {}
    
    };
    

    See http://www.sgi.com/tech/stl/stl_vector.h


    But the more important is that you should not define your vector like this at all:

    template <typename T, typename A = mmap_allocator<T> >
        class mmappable_vector: public std::vector<T, A> {
                ...
        };
    

    It is wrong because it derives from STL container, derivation is public and you do not have virtual destructor.


    As far as I understand your question – you just need a typedef. There are two ways to make typedef in C++ – C++11 and C++03 ways:

    C++11

    template< typename T, typename A = mmap_allocator<T> >
    using mmappable_vector = std::vector<T, A>;
    

    C++03

        template <typename T, typename A = mmap_allocator<T> >
        struct mmappable_vector {
            typedef std::vector<T, A> type;
        };
    

    Use it as:

        mmappable_vector<int>::type
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a class encapsulating std container: class Stash { list<int> Data; public:
Suppose we have an abstract class Element from which classes Triangle and Quadrilateral are
I have a question about template classes. For example, take this class template<class TBase>
My problem is quite simple Suppose I have those class: public class A {
My question is that suppose i have an asp:textbox and i do not specify
I am new to CSharp.I have seen this() in some code.My question is Suppose
I have a very basic question... Suppose I can get a closing price for
i have a question regarding partial page loading with AJAX. Suppose that an user
I have a simple question. I have to create objects which extend DataObject, but
What a title, suppose I have a map like this: std::map<int, int> m; 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.