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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:44:24+00:00 2026-05-20T04:44:24+00:00

It just occurred to me I noticed that std::string ‘s substr operation could be

  • 0

It just occurred to me I noticed that std::string‘s substr operation could be much more efficient for rvalues when it could steal the allocated memory from *this.

The Standard library of N3225 contains the following member function declaration of std::string

basic_string substr(size_type pos = 0, size_type n = npos) const;

Can an implementation that could implement an optimized substr for rvalues overload that and provide two versions, one of which could reuse the buffer for rvalue strings?

basic_string substr(size_type pos = 0) &&;
basic_string substr(size_type pos, size_type n) const;

I imagine the rvalue version could be implemented as follows, reusing the memory of *this an setting *this to a moved-from state.

basic_string substr(size_type pos = 0) && {
  basic_string __r;
  __r.__internal_share_buf(pos, __start + pos, __size - pos);
  __start = 0; // or whatever the 'empty' state is
  return __r;
}

Does this work in an efficient fashion on common string implementations or would this take too much housekeeping?

  • 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-05-20T04:44:24+00:00Added an answer on May 20, 2026 at 4:44 am

    Firstly, an implementation cannot add an overload that steals the source, since that would be detectable:

    std::string s="some random string";
    std::string s2=std::move(s).substr(5,5);
    assert(s=="some random string"); 
    assert(s2=="rando");
    

    The first assert would fail if the implementation stole the data from s, and the C++0x wording essentially outlaws copy on write.

    Secondly, this wouldn’t necessarily be an optimization anyway: you’d have to add additional housekeeping in std::string to handle the case that it’s a substring of a larger string, and it would mean keeping large blocks around when there was no longer any strings referencing the large string, just some substring of it.

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

Sidebar

Related Questions

So it just occurred to me that once I upload profile pics to S3,
I was just reading this thread and it occurred to me that there is
Just how much slower are events? I have written a streaming XML parser (that
I've just noticed that Array doesn't override the triple equal sign method === ,
It just occurred to me That I don't know how to initialize a pointer
I've always just used OpenOffice Draw and it just occurred to me that there
Using Visual Studio 2005, the debugger tells me that a deadlock has occurred just
I'm new to JS (from C++/etc), and it's just occurred to me that closures
It has just occurred to me that when my Flex application does a ChannelSet.login,
I just started using Linq to SQL, and it just occurred to me that

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.