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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:50:16+00:00 2026-05-20T22:50:16+00:00

In A Brief Introduction to Rvalue References , forward is defined as follows: template

  • 0

In A Brief Introduction to Rvalue References, forward is defined as follows:

  template <typename T>
  struct identity { typedef T type; };

  template <typename T>
  T &&forward(typename identity<T>::type &&a) { return a; }

What purpose does the identity class perform? Why not:

  template <typename T>
  T &&forward(T &&a) { return a; }
  • 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-20T22:50:16+00:00Added an answer on May 20, 2026 at 10:50 pm

    The purpose of identity was to make T non-deducible. That is, to force the client to explicitly supply T when calling forward.

    forward(a);     // compile-time error
    forward<A>(a);  // ok
    

    The reason this is necessary is because the template parameter is the switch with which the client tells the compiler to forward the argument as either an lvalue or as an rvalue. If you accidentally forget to supply this information then lvalues are always returned as lvalues and rvalues are always returned as rvalues. While at first that may sound like what you want, it really isn’t.

    template <class T, class A1>
    std::shared_ptr<T>
    factory(A1&& a1)
    {
        return std::shared_ptr<T>(new T(std::forward<A1>(a1)));
    }
    

    In the above example a1 is always an lvalue. But the “switch” A1 may or may not be an lvalue reference. If it is an lvalue reference, a1 gets returned as an lvalue, otherwise a1 gets returned as an rvalue. If the author of factory accidentally forgets to supply A1, the use of identity reminds him at compile time.

    Note: The final draft lacks identity, but uses remove_reference in the same place for the same purpose.

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

Sidebar

Related Questions

There's only a brief introduction to Chameleon Template in document of pyramid. Any detailed
If I define a function which accepts an rvalue reference parameter: template <typename T>
What is a brief introduction to lexical scoping?
Brief introduction: I have this ASP.NET Webforms site with the particularity that it doesn't
Recently, while I was writing a brief introduction to C for Fortran programmers, one
I received a brief introduction to ASP.NET MVC in school and I am trying
BRIEF: hg clone creates path default in /.hg/hgrc, set to where you cloned from.
Brief Context: Hi, I am a university student (behind proxy 10.3.100.211:8080), new to ROR,
Brief question What command can I use to make my DataSet refresh it's connection
Brief Background I'm investigating the potential of investing in Agile Toolkit for a future

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.