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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:37:30+00:00 2026-05-28T04:37:30+00:00

In VS2010 std::forward is defined as such: template<class _Ty> inline _Ty&& forward(typename identity<_Ty>::type& _Arg)

  • 0

In VS2010 std::forward is defined as such:

template<class _Ty> inline
_Ty&& forward(typename identity<_Ty>::type& _Arg)
{   // forward _Arg, given explicitly specified type parameter
    return ((_Ty&&)_Arg);
}

identity appears to be used solely to disable template argument deduction. What’s the point of purposefully disabling it in this case?

  • 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-28T04:37:31+00:00Added an answer on May 28, 2026 at 4:37 am

    If you pass an rvalue reference to an object of type X to a template function that takes type T&& as its parameter, template argument deduction deduces T to be X. Therefore, the parameter has type X&&. If the function argument is an lvalue or const lvalue, the compiler deduces its type to be an lvalue reference or const lvalue reference of that type.

    If std::forward used template argument deduction:

    Since objects with names are lvalues the only time std::forward would correctly cast to T&& would be when the input argument was an unnamed rvalue (like 7 or func()). In the case of perfect forwarding the arg you pass to std::forward is an lvalue because it has a name. std::forward‘s type would be deduced as an lvalue reference or const lvalue reference. Reference collapsing rules would cause the T&& in static_cast<T&&>(arg) in std::forward to always resolve as an lvalue reference or const lvalue reference.

    Example:

    template<typename T>
    T&& forward_with_deduction(T&& obj)
    {
        return static_cast<T&&>(obj);
    }
    
    void test(int&){}
    void test(const int&){}
    void test(int&&){}
    
    template<typename T>
    void perfect_forwarder(T&& obj)
    {
        test(forward_with_deduction(obj));
    }
    
    int main()
    {
        int x;
        const int& y(x);
        int&& z = std::move(x);
    
        test(forward_with_deduction(7));    //  7 is an int&&, correctly calls test(int&&)
        test(forward_with_deduction(z));    //  z is treated as an int&, calls test(int&)
    
        //  All the below call test(int&) or test(const int&) because in perfect_forwarder 'obj' is treated as
        //  an int& or const int& (because it is named) so T in forward_with_deduction is deduced as int& 
        //  or const int&. The T&& in static_cast<T&&>(obj) then collapses to int& or const int& - which is not what 
        //  we want in the bottom two cases.
        perfect_forwarder(x);           
        perfect_forwarder(y);           
        perfect_forwarder(std::move(x));
        perfect_forwarder(std::move(y));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following when building a graph. #include <vector> using namespace std; template<class
I'm converting a project from vs2008 to vs2010 and getting linker errors for std:ifstream/ofstream
I'm using VS2010 Beta 2, I have a Complex Type called Address with the
I tried to switch from std::unordered_map (VS2010) to boost::unordered_map (version 1.48) and surprisingly, some
Let's say I have: class myClass std::list<myClass> myList where myClass does not define the
This code fails to compile under VS2010: #include <functional> using namespace std; void test()
I'm using VS2010. In the constructor of my non-compyable Scene class I have :
So on my VS2010 I can compile code like : boost::shared_ptr<boost::thread> internal_thread; boost::packaged_task<void> internal_task_w(boost::bind(&thread_pool::internal_run,
#include <vector> using std::vector; class A { public: A() { buf.push_back(65); buf.push_back(66); buf.push_back(67); }
I have two containers, let's say they're defined like this: std::vector<std::unique_ptr<int>> a; std::vector<std::unique_ptr<int>> b;

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.