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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:46:45+00:00 2026-05-24T19:46:45+00:00

I have the habit (?!?!?) of returning everything as a const value. Like this…

  • 0

I have the habit (?!?!?) of returning everything as a “const” value. Like this…

struct s;

s const make_s();

s const &s0 = make_s();
s const s1 = make_s();

With move operations and r-value references and the following functions…

void take_s(s &&s0);
void take_s(s const &&s0);  //  Doesn't make sense

I can no longer write…

take_s(make_s());

The main reason I started using the convention of returning const values is to prevent someone from writing code like this…

make_s().mutating_member_function();

The use case is the following…

struct c_str_proxy {
    std::string m_s;

    c_str_proxy(std::string &&s) : m_s(std::move(s)) {
    }
};

c_str_proxy c_str(std::string &&s) {
    return c_str_proxy(s);
}

char const * const c_str(std::string const &s) {
    return s.c_str();
}

std::vector < std::string > const &v = make_v();
std::puts(c_str(boost::join(v, ", ")));

std::string const my_join(std::vector < std::string > const &v, char const *sep);

//  THE FOLLOWING WORKS, BUT I THINK THAT IS ACCIDENTAL
//  IT CALLS
//
//      c_str(std::string const &);
//
//  BUT I THINK THE TEMPORARY RETURNED BY
//
//      my_join(v, "; ")
//
//  IS NO LONGER ALIVE BY THE TIME WE ARE INSIDE
//
//      std::puts
//
//  AS WE ARE TAKING THE "c_str()" OF A TEMPORARY "std::string"
//
std::puts(c_str(my_join(v, "; ")));

Looks as if “returning const value” and r-value references don’t mix in this particular use case. Is that right?

**Edit 0: Extra question...**

The object is temporary anyway. Why should “const” prevent moving? Why can’t we move “const” temporaries?

  • 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-24T19:46:46+00:00Added an answer on May 24, 2026 at 7:46 pm

    You have two conflicting goals. On one hand, you want to prevent modifications from being made to the returned object, but on the other hand, you want to allow modifications to be made (that’s what a move operation is. It modifies the source object, by stealing its internal resources).

    You need to make up your mind. Do you want the object to be immutable, or do you want people to be able to modify it?

    For what it’s worth, I don’t really see what you’d gain by returning const temporaries in the first place. Yes, you prevent people from calling a mutating member function on it, but why would you want to? At best, being able to do so can be useful, and at worst, it’s a mistake that is easily avoided.

    And what you’re doing doesn’t really make much sense. The entire point in a temporary is that it’s going to go away in a moment, so who cares if it gets modified?

    The entire idea behind rvalue references and move semantics is that temporaries are temporary, and so they can be modified without harming anyone.

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

Sidebar

Related Questions

I have this horrible habit of typing the below and not catching it until
I'm coming from predominantly using VIM for everything, and so I have a habit
I've always wondered this. I have a habit of always adding use strict; use
I have this habit always a C++ project is compiled and the release is
I have a habit of hardcoding URLs into my HTML: ...<a href=www.mySite.com/login>logon to your
Some of the developers on the project I work on have a habit of
Coming from C/C++ a long time ago I still have a habit of ensuring
Problem Overview: My application has an enrollment form. Users have a habit of entering
I've got the design habit to have a SqlConnection object populated by a child
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css

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.