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

  • Home
  • SEARCH
  • 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 7896797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:59:12+00:00 2026-06-03T07:59:12+00:00

This example on the usage of std::forward is puzzling me. This is my edited

  • 0

This example on the usage of std::forward is puzzling me. This is my edited version:

#include <iostream>
#include <memory>
#include <utility>
using namespace std;

struct A{
    A(int&& n) { cout << "rvalue overload, n=" << n << "\n"; }
    A(int& n)  { cout << "lvalue overload, n=" << n << "\n"; }
};

template<typename> void template_type_dumper();

template<class T, class U>
unique_ptr<T> make_unique(U&& u){
    //Have a "fingerprint" of what function is being called
    static int dummyvar;
    cout<<"address of make_unique::dummyvar: "<<&dummyvar<<endl;
    //g++ dumps two warnings here, which reveal what exact type is passed as template parameter
    template_type_dumper<decltype(u)>;
    template_type_dumper<U>;

    return unique_ptr<T>(new T(forward<U>(u)));
}

int main()
{
    unique_ptr<A> p1 = make_unique<A>(2); // rvalue
    int i = 1;
    unique_ptr<A> p2 = make_unique<A>(i); // lvalue
}

The output is

address of make_unique::dummyvar: 0x6021a4
rvalue overload, n=2
address of make_unique::dummyvar: 0x6021a8
lvalue overload, n=1

and the warnings about reference to template_type_dumper show that in the first instantiation, decltype(u) = int&& and U = int, for the second decltype(u) = int& and U = int&.

It’s evident that there are two different instantiations as expected, but her are my questions:

  1. how can std::forward work here? In the first instantiation, its template argument is explicitly U = int, how can it know that it has to return a rvalue-reference? What would happen if I specified U&& instead?
  2. make_unique is declared to take a rvalue-reference. How come u can be a lvalue-reference? Is there any special rule that I am missing?
  • 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-03T07:59:13+00:00Added an answer on June 3, 2026 at 7:59 am

    make_unique is declared to take a rvalue-reference. How come u can be a lvalue-reference? Is there any special rule that I am missing?

    make_unique is declared to take a reference. What kind that reference is is to be deduced. If an lvalue of type foo is passed, U is deduced as foo& and U&& becomes foo& because of the reference collapsing rules (basically, “combining” an lvalue reference with another reference always produces an lvalue reference; combining two rvalue references produces an rvalue reference). If an rvalue of type foo is passed, U is deduced as foo and U&& is foo&&.

    This is one of the things that powers perfect forwarding: with U&& you can take both lvalues and rvalues, and U is deduced to match the appropriate value category. Then with std::forward you can forward the values preserving that same value category: in the first case, you get std::forward<foo&> which forwards an lvalue, and in the second one, you get std::forward<foo> which forwards an rvalue.

    In the first instantiation, its template argument is explicitly U = int, how can it know that it has to return a rvalue-reference?

    Because the return type of std::forward<T> is always T&&. If you pass int it returns int&&. If you pass int& it returns int& again because of the reference collapsing rules.

    What would happen if I specified U&& instead?

    You would have std::forward<int&&> and the reference collapsing rules make int&& && an rvalue reference still: int&&.

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

Sidebar

Related Questions

I learned an example of usage of sockets. In this example a client sends
Please give me some examples of jump table usage. I have seen this example
I've found this cool example of border-color 's transparent property usage to make an
I understand that this kind of identification cannot be 100% accurate, but example usage
What does this symbol mean in PHP <?= ? Example usage: <h2>Manage Role: >
I want to create something like this dup.bat infile outfile times example usage would
I am using an example program from this code http://sicktoolbox.sourceforge.net/ > http://sourceforge.net/projects/sicktoolbox/files/ . It's
Here is some code I wrote (using GCC's __restrict__ extension to C++): #include <iostream>
Update: This issue is caused by bad memory usage, see solution at the bottom.
I am looking for someones opinion about the usage of std::logic_error instead of using

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.