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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:59:25+00:00 2026-06-06T00:59:25+00:00

Let’s assume that we need some class for wrapping std::string, and besides all other

  • 0

Let’s assume that we need some class for wrapping std::string, and besides all other details, it provides an automatic conversion back to std::string, using a type cast operator:

class MyWrappedString {
    std::string m_value;
    /* ... */
public:
    inline operator std::string() const {
        return m_value;
    }
};

So, the operator will return a copy of the wrapped string object.

But, why is the following code seemingly correct?

MyWrappedString x;
const std::string& y = x;
// now, y should be a reference to a temporary, right?
std::cout << "y is: " << y << std::endl;

The conversion operator will return a temporary copy of m_value, so const std::string& y = x will create a reference to that temporary copy.

Why does this work? I remember there was some kind of extension of lifetime of referenced objects, but I am not sure.

And second question: is it possible to have a type cast operator that returns a const reference?

E.g.:

inline operator const std::string &() const {
    return m_value;
}

So, that the above code does not have to work on a temporary copy?

PS: This question is a bit related to: Lifetime of temporaries, but still a different issue.

  • 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-06T00:59:28+00:00Added an answer on June 6, 2026 at 12:59 am

    const references are keeping the reference alive (even if it would have gone out out of scope normally) until the const reference goes out of scope

    For the second question: yes, you can return a const reference, and the return value from the function will have to be assinged to a const reference

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

Sidebar

Related Questions

Let's assume that a user votes for some movies in a scale of 1
Let simplify the question: All I need is to explode() string by a comma
Let me start by telling you that I never used anything besides SVN and
Let's assume that I am at manual A. I press enter and I am
Let's assume that we are building a high traffic site that will be used
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Let's say I have the following classes : public class MyProductCode { private String
Let assume we have two activities. A - main activity, that is home launcher
Let's say I got a Map<String, String> and I wanna remove all the entries
Let's say I have the string: hello world; some random text; foo; How could

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.