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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:46:23+00:00 2026-05-19T09:46:23+00:00

sorry for such a long question but I try to be as clear as

  • 0

sorry for such a long question but I try to be as clear as possible. This somehow follows my previous question about strings in C++. I’m trying to figure out how I could return std::string from a function without redundant memory allocations, without relying on NRVO. The reasons why I don’t want to rely on NRVO are:

  • it is not supported by the compiler we currently use
  • even when it is supported it might not always be enabled in Debug mode
  • it might fail in some cases (example)

Please note that I need a C++03 compatible solution (no C++0x rvalue references thus, unfortunately…)

The simplest way do this is pass-by-reference and do std::swap, like this

void test(std::string& res)
{
    std::string s;
    //...
    res.swap(s);
}

But it is more natural and often more convenient to return by value than pass by reference, so what I want to achieve is this:

std::string test()
{
    std::string s;
    //...
    return SOMETHING(s);
}

Ideally it would just do a swap with the “return value”, but I don’t see how to do this in C++. There is auto_ptr already which does move instead of copy, and I could actually use auto_ptr<string>, but I’d like to avoid dynamically allocating the string object itself.

My idea is to somehow “tag” a string object that it is being returned from a function to permit moving its data when a copy constructor is called on return. So I ended up with this code, which does exactly what I want:

struct Str
{
    struct Moveable
    {
        Str & ref;
        explicit Moveable(Str & other): ref(other) {}
    };

    Str() {}
    Str(const std::string& other) : data(other) {} // copy
    Str(Moveable& other) { data.swap(other.ref.data); } // move

    Moveable Move()
    {
        return Moveable(*this);
    }

    std::string data;
};

Str test()
{
    Str s;
    //...
    return s.Move(); // no allocation, even without NRVO
}

So… Does all this make sense, or there are some serious issues that I’m missing? (I’m not sure if there is no lifetime issue for example). Maybe you have already seen such idea in a library (book, article…), and could give me a reference to it?

EDIT: As @rstevens noticed, this code is MSVC-specific and won’t compile under g++ which does not like the non-const temporary. This is an issue, but let’s just assume this implementation is MSVC-specific.

  • 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-19T09:46:24+00:00Added an answer on May 19, 2026 at 9:46 am

    The implementation of boost uses move semantics emulation internally for libraries like Boost.Thread. You may want to look at the implementation and do something similar.

    Edit: there is actually an active development of a library Boost.Move, so you can already start using it.

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

Sidebar

Related Questions

Sorry this is such a long question but it touches on a general issue
Sorry for such a trivial question, but I'd feel silly building this if it
sorry this is such a simple question but I can't figure it out. How
My question (which will follow after this, sorry about the long intro, the question
Sorry for asking such a beginner question but I can't figure this out. I
Sorry for such a silly question but Python docs are confusing... Link 1: Queue
Sorry to ask such a noob question, but the NDK documentation is wrong (r7b):
I am very sorry to ask such a basic question but I am new
I'm sorry for maybe making such a basic question but in ASP.NET websites what
Sorry for a long question and not a very descriptive title, but my problem

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.