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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:34:30+00:00 2026-06-10T00:34:30+00:00

To my knowledge, returning *this is the norm of writing overloaded operator= . But

  • 0

To my knowledge, returning *this is the norm of writing overloaded operator=. But this may promote a rvalue to a lvalue!

struct Foo {
  Foo& operator=(const Foo &t) { return *this; }
};

int main() {
  const Foo &ref = Foo();
  //Foo &ref1 = Foo();  // This line won't compile.
  //But the following line compiles. Isn't it dangerous?
  //Also please note that if = is not overloaded, the synthesized operator= will NOT let it compile.
  Foo &ref2 = (Foo() = Foo());

  return 0;
}
  • 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-10T00:34:32+00:00Added an answer on June 10, 2026 at 12:34 am

    You’re right. The short answer is that this is legal, but is dangerous for the reasons you point out.

    Another (similar) example was added to C++11, as part of the standard no less, with the rvalue stream output, which returns an lvalue reference.

    template <class charT, class traits, class T>
    basic_ostream<charT, traits>&
    operator<<(basic_ostream<charT, traits>&& os, const T& x);
    

    So std::ostream& s = std::ofstream("foo.txt") << "Oh boy"; is legal, but creates a dangling reference.

    I believe that C++11 compilers will allow member functions to be restricted to the case when the object is an lvalue, which should allow you to prevent the problem at all. The below will only allow the assignment when *this is an lvalue, so it prevents you from converting by accident.

    struct Foo {
      Foo& operator=(const Foo &t) & { return *this; }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I may get negative marks for this but still getting some comments from experts
My knowledge of xml and php is somewhat basic but have been able to
My XQuery knowledge is pretty lacking but I'm trying to play around with XQIB
My knowledge of C++ at this point is more academic than anything else. In
My JavaScript knowledge is shallow, so I can't figure this out after some significant
I'm building a matrix class to reinforce my knowledge in c++. My overloaded ==
Not sure why... but my previously functional push notification registration callback is returning a
I would like to make a reservations formula but I have very low knowledge
Not quite sure how to tackle this one as my knowledge is limited. However
I've reached the end of my limited knowledge with this problem. At the moment,

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.