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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:17:17+00:00 2026-06-03T20:17:17+00:00

So I have a nice persistent allocator class persistent_alloc<T> that allows me to allocate

  • 0

So I have a nice persistent allocator class persistent_alloc<T> that allows me to allocate C++ container objects and strings in persistent memory which is backed by an mmaped file that can persist from one run of my program to the next.

My problem comes when I want to do anything that mixes persistent and non persistent objects. For example, I have

typedef std::basic_string<char, std::char_traits<char>, persistent_alloc<char>> pstring;

pstring a, b, c;
std::string x, y, z;

I want to be able to do things like:

if (a == x)
    a = y;
c = z + b;

and so forth, but by default it does not work, as pstring and std::string are unrelated types. Now as far as the comparison is concerned, I can define:

template<typename Alloc1, typename Alloc2> inline bool
operator==(const std::basic_string<char, std::char_traits<char>, Alloc1> &a,
           const std::basic_string<char, std::char_traits<char>, Alloc2> &b)
{
    return strcmp(a.c_str(), b.c_str()) == 0;
}

…and now I can compare strings for equality. But adding these for every operation seems like a pain — it seems like they SHOULD be provided by the standard library. Worse, assignment operators and copy constructors must be members and can’t be defined as global inline functions like this.

Is there a reasonable way of doing this? Or do I have to effectively rewrite the entire standard library to support allocators usefully?

  • 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-03T20:17:19+00:00Added an answer on June 3, 2026 at 8:17 pm

    There is a way to handle this, but you need to think outside of the box a bit. What you need is an intermediate type that is implicitly constructable from both std::string and your allocator string.

    There is a proposal for such a thing before the C++ committee currently. It is based on a Google-built Apache-licensed implementation that already exists. It’s called basic_string_ref; it’s a template class that is basically a pointer to the first character in a string and a size, representing the length of the string. It’s not a true container in the sense that it doesn’t manage memory.

    Which is exactly what you need.

    basic_string_ref for a particular character type and traits type is implicitly constructable from a std::basic_string regardless of allocator.

    All of the comparison operators can be defined in terms of basic_string_ref. Since it is implicitly constructable from std::basic_string (and virtually free to construct), it would work transparently for comparisons between differently allocated strings.

    Doing assignment is rather trickier, but doable. It requires a series of conversions:

    a = pstring{basic_string_ref{y}};
    

    Not the prettiest code, of course. We would prefer to simply change the copy constructor and assignment operator of std::basic_string to be allocator agnostic. But since that’s not doable, this is really the next best thing. You could even wrap it in a template function:

    template<typename DestAllocator, typename SourceAllocator, typename charT, typename traits>
    std::basic_string<charT, traits, DestAllocator> conv_str(const std::basic_string<charT, traits, SourceAllocator> &input)
    {
      return std::basic_string<charT, traits, DestAllocator>{basic_string_ref<charT, traits>{y}};
    }
    

    Of course, if you can do that, you can just do this:

    template<typename DestAllocator, typename SourceAllocator, typename charT, typename traits>
    std::basic_string<charT, traits, DestAllocator> conv_str(const std::basic_string<charT, traits, SourceAllocator> &input)
    {
      return std::basic_string<charT, traits, DestAllocator>{y.begin(), y.end()};
    }
    

    It’d be great if this were just part of std::basic_string, so that you wouldn’t need the workarounds. But it isn’t.

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

Sidebar

Related Questions

Imagine that I have a nice Deck class, in the best OO fashion. It
I have a nice page that does everything I need. However one of the
I have this nice little MSBuild-based daily build setup that I use on my
I have a nice iPhone app built that uses a UINavigationController to navigate through
In vim, I can have a nice small window that lists all my open
I am targeting 1.6 but I would like to have nice widget that can
I have a nice DataGridView showing what is basically some kind of log data
I have a nice interface, and I want to implement one member of it
Does anyone have a nice solution for GalleryView when I want to display photos
So I have this nice spiffy MVC-architected application in Java Swing, and now I

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.