Home > Backend Development > C++ > body text

Why does std::forward use the identity template to disable template argument deduction?

Susan Sarandon
Release: 2024-11-09 09:34:02
Original
654 people have browsed it

Why does std::forward use the identity template to disable template argument deduction?

Disabling Template Argument Deduction with std::forward to Ensure Correct Forwarding

Consider the definition of std::forward in VS2010:

<code class="cpp">template<class _Ty> inline
_Ty&amp;&amp; forward(typename identity<_Ty>::type&amp; _Arg)
{   // forward _Arg, given explicitly specified type parameter
    return ((_Ty&amp;&amp;)_Arg);
}</code>
Copy after login

The purpose of the identity template is to disable template argument deduction. Why is this crucial in this scenario?

Template argument deduction would lead to incorrect type deduction. If an rvalue reference to an object of type X is passed to a template function with a parameter type T&, template argument deduction would infer T as X, resulting in a parameter type X&. However, for perfect forwarding, the parameter is an lvalue because it has a name. Therefore, using template argument deduction in std::forward would cause the deduced parameter type to be an lvalue reference or const lvalue reference.

<code class="cpp">template<typename T>
T&amp;&amp; forward_with_deduction(T&amp;&amp; obj)
{
    return static_cast<T&amp;&amp;>(obj);
}</code>
Copy after login

Consider the following example:

<code class="cpp">void test(int&amp;){}
void test(const int&amp;){}
void test(int&amp;&amp;){}

template<typename T>
void perfect_forwarder(T&amp;&amp; obj)
{
    test(forward_with_deduction(obj));
}

int main()
{
    int x;
    const int&amp; y(x);
    int&amp;&amp; z = std::move(x);

    test(forward_with_deduction(7));    //  7 is an int&amp;&amp;, correctly calls test(int&amp;&amp;)
    test(forward_with_deduction(z));    //  z is treated as an int&amp;, calls test(int&amp;)

    //  All the below call test(int&amp;) or test(const int&amp;) because in perfect_forwarder 'obj' is treated as
    //  an int&amp; or const int&amp; (because it is named) so T in forward_with_deduction is deduced as int&amp; 
    //  or const int&amp;. The T&amp;&amp; in static_cast<T&amp;&amp;>(obj) then collapses to int&amp; or const int&amp; - 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));
}</code>
Copy after login

In this example, perfect forwarding fails because the parameter in perfect_forwarder is treated as an lvalue or const lvalue reference due to its name. This leads to incorrect type deduction in forward_with_deduction, resulting in undesired static_cast semantics.

Disabling template argument deduction with the identity template in std::forward ensures that std::forward always returns an rvalue reference, which is essential for correct perfect forwarding of lvalues as well as rvalues.

The above is the detailed content of Why does std::forward use the identity template to disable template argument deduction?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template