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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:00:40+00:00 2026-06-13T12:00:40+00:00

Possible Duplicate: Should I return const objects? (The original title of that question was:

  • 0

Possible Duplicate:
Should I return const objects?
(The original title of that question was: int foo() or const int foo()? explaining why I missed it.)


Effective C++, Item 3: Use const whenever possible. In particular, returning const objects is promoted to avoid unintended assignment like if (a*b = c) {. I find it a little paranoid, nevertheless I have been following this advice.

It seems to me that returning const objects can degrade performance in C++11.

#include <iostream>
using namespace std;

class C {
public:
    C() : v(nullptr) { }

    C& operator=(const C& other) {
        cout << "copy" << endl;
        // copy contents of v[]
        return *this;
    }

    C& operator=(C&& other) {
        cout << "move" << endl;
        v = other.v, other.v = nullptr;
        return *this;
    }

private:
    int* v;
};

const C const_is_returned() { return C(); }

C nonconst_is_returned() { return C(); }

int main(int argc, char* argv[]) {
    C c;
    c = const_is_returned();
    c = nonconst_is_returned();
    return 0;
}

This prints:

copy
move

Do I implement the move assignment correctly? Or I simply shouldn’t return const objects anymore in C++11?

  • 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-13T12:00:41+00:00Added an answer on June 13, 2026 at 12:00 pm

    Returning const objects is a workaround that might cause other problems. Since C++11, there is a better solution for the assignment issue: Reference Qualifiers for member functions. I try to explain it with some code:

    int foo(); // function declaration
    foo() = 42; // ERROR
    

    The assignment in the second line results in a compile-time error for the builtin type int in both C and C++. Same for other builtin types. That’s because the assignment operator for builtin types requires a non-const lvalue-reference on the left hand side. To put it in code, the assignment operator might look as follows (invalid code):

    int& operator=(int& lhs, const int& rhs);
    

    It was always possible in C++ to restrict parameters to lvalue references. However, that wasn’t possible until C++11 for the implicit first parameter of member functions (*this).

    That changed with C++11: Similar to const qualifiers for member functions, there are now reference qualifiers for member functions. The following code shows the usage on the copy and move operators (note the & after the parameter list):

    struct Int
    {
        Int(const Int& rhs) = default;
        Int(Int&& rhs) noexcept = default;
        ~Int() noexcept = default;
        auto operator=(const Int& rhs) & -> Int& = default;
        auto operator=(Int&& rhs) & noexcept -> Int& = default;
    };
    

    With this class declaration, the assignment expression in the following code fragment is invalid, whereas assigning to a local variable works – as it was in the first example.

    Int bar();
    Int baz();
    bar() = baz(); // ERROR: no viable overloaded '='
    

    So there is no need to return const objects. You can restrict the assigment operators to lvalue references, so that everything else still works as expected – in particular move operations.

    See also:

    • What is "rvalue reference for *this"?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What should main() return in C/C++? why do we give int main
Possible Duplicate: What should main() return in C/C++? Difference between void main and int
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
Possible Duplicate: SWIG Java Retaining Class information of the objects bouncing from C++ Question
Possible Duplicate: What should main() return in C/C++? What does main return? I've been
Possible Duplicate: Counting inversions in an array This is a programming question that I
Possible Duplicate: Error handling in C code What return value should you use for
Possible Duplicate: Should accessors return values or constant references? First of all, let's ignore
Possible Duplicate: Should a function have only one return statement? This is what I
Possible Duplicate: How should I store GUID in MySQL tables? VARCHAR will return as

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.