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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:20:12+00:00 2026-06-18T08:20:12+00:00

As we have known, in most common cases, T&& means this is a temporary

  • 0

As we have known, in most common cases, T&& means “this is a temporary object”. However, if one wants to return a temporary object from a function, he/she can declare the function as follows:

template<class T>
T f()
{
    T t;
    ......

    return t;
}

or (Note: Not Correct)

template<class T>
T&& f()
{
    T t;
    ......

    return t;
}

But I think the latter is overdid, because the former is enough and backward compatible.

Yet, I also find the std::forward()‘s return type is declared as T&&, so I’m sure my understanding about this is incomplete.

My real question is: When and where should we declare the return type of a function as T&&?

  • 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-18T08:20:13+00:00Added an answer on June 18, 2026 at 8:20 am

    In your example, the T&& is wrong, it’s a dangling reference.

    But std::forward doesn’t return an rvalue reference to a local variable in its own definition, it returns an rvalue reference to its by-rvalue-reference argument (or an lvalue reference to a by-lvalue-reference argument).

    You should return an rvalue reference only if you want the caller of your function to be able to move from whatever that reference refers to.

    Normally that will only be if the purpose of the function is to provide move access to some significant object (perhaps which already exists). So that includes std::move (which allows you to move from an lvalue), and similarly you might write an accessor function specifically designed for users to move from a data member of some object, or an element of some container. If the object itself isn’t significant, only the value, then you can return by value.

    As grizzly says, sometimes due to reference collapsing you can take advantage of tricks which mean that you type T&& in your code, but when T is already an lvalue-reference type T&& is the same lvalue reference type. std::forward uses that trick. That is to say, because of reference collapsing T&& doesn’t mean “rvalue reference to T”, it means “T if T is a reference type, otherwise rvalue reference to T”.

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

Sidebar

Related Questions

As you may have known that I switched from ubuntu to windows from my
Scala programmer should have known that this sort of writing : class Person{ var
I have a class ( MyClass ) that inherits most of its functionality from
Wikipedia provides information about one of the most common scenarios for exploiting a reflected
I know that most people have DirectX9-capable cards but is it really necessary to
World's most convuluted title I know, an example should explain it better. I have
I have a known shared folder on another Windows PC and i have to
I have a variable known as $SessionMinus. What I want to do is that
I have completed the well known 'news' toutorial included in CI documentation. sometimes double
I have a rectangle which has known width and height.Now devide rectangle diagonally to

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.