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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:42:54+00:00 2026-06-12T19:42:54+00:00

In the lecture about universal references , Scott Meyers (at approximately 40th minute) said

  • 0

In the lecture about universal references, Scott Meyers (at approximately 40th minute) said that objects that are universal references should be converted into real type, before used. In other words, whenever there is a template function with universal reference type, std::forward should be used before operators and expressions are used, otherwise a copy of the object might be made.

My understanding of this is in the following example :

#include <iostream>

struct A
{
  A() { std::cout<<"constr"<<std::endl; }
  A(const A&) { std::cout<<"copy constr"<<std::endl; }
  A(A&&) { std::cout<<"move constr"<<std::endl; }
  A& operator=(const A&) { std::cout<<"copy assign"<<std::endl; return *this; }
  A& operator=(A&&) { std::cout<<"move assign"<<std::endl; return *this; }

  ~A() { std::cout<<"destr"<<std::endl; }

  void bar()
  {
    std::cout<<"bar"<<std::endl;
  }
};

A getA()
{
  A a;
  return a;
}

template< typename T >
void callBar( T && a )
{
  std::forward< T >( a ).bar();
}

int main()
{
  {
    std::cout<<"\n1"<<std::endl;
    A a;
    callBar( a );
  }

  {
    std::cout<<"\n2"<<std::endl;
    callBar( getA() );
  }
}

As expected, the output is :

1
constr
bar
destr

2
constr
move constr
destr
bar
destr

The question really is why is this needed?

std::forward< T >( a ).bar();

I tried without std::forward, and it seems to work fine (the output is the same).

Similarly, why he recommends to use move inside the function with rvalue? (the answer is the same as for std::forward)

void callBar( A && a )
{
  std::move(a).bar();
}

I understand that both std::move and std::forward are just casts to appropriate types, but are these casts really needed in the above example?

Bonus : how can the example be modified to produce the copy of the object that is passed to that function?

  • 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-12T19:42:55+00:00Added an answer on June 12, 2026 at 7:42 pm

    What is said in the lecture is this :

    void doWork( Widget&& param )
    {
      ops and exprs using std::move(param)
    }
    

    SM: What this means is : if you see code that takes a rvalue reference, and you see use of that parameter without being wrapped by move, it is highly suspect.

    After some thought, I realized that it is correct (as expected). Changing the callBar function in the original example to this demonstrate the point :

    void reallyCallBar( A& la )
    {
      std::cout<<"lvalue"<<std::endl;
      la.bar();
    }
    
    void reallyCallBar( A&& ra )
    {
      std::cout<<"rvalue"<<std::endl;
      ra.bar();
    }
    
    template< typename T >
    void callBar( T && a )
    {
      reallyCallBar( std::forward< T >( a ) );
    }
    

    If the std::forward wasn’t used in callBar, then the reallyCallBar( A& ) would be used. Because a in callBar is a lvalue reference. std::forward makes it a rvalue, when the universal reference is the rvalue reference.

    Next modification proves the point even further :

    void reallyCallBar( A& la )
    {
      std::cout<<"lvalue"<<std::endl;
      la.bar();
    }
    
    void reallyCallBar( A&& ra )
    {
      std::cout<<"rvalue"<<std::endl;
      reallyCallBar( ra );
    }
    
    template< typename T >
    void callBar( T && a )
    {
      reallyCallBar( std::forward< T >( a ) );
    }
    

    Since std::move is not used in the reallyCallBar( A&& ra ) function, it doesn’t enter the endless loop. Instead it calls the version taking lvalue reference.

    Therefore (as explained in the lecture) :

    • std::forward must be used on universal references
    • std::move must be used on rvalue references
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After Honeycomb, Google said that bitmaps are managed by the heap (talked about here
Some time ago (~4-5months ago) I attented a lecture about Java EE and at
I'm working on a simple spell checking app for a lecture that I'm giving.
In this video, taken from Stanford's CS107 lecture, the professor seems to state that
My question is about Odersky's lecture 3.5 (not the assignment!): The exercise at 19:15
At Computer Science lecture we’ve been told that there’s a special code (lower that
Below is a lecture slide about AI. I think it's a pseudo code about
The question posed came about during a 2nd Year Comp Science lecture while discussing
A question about C++ programming. Can you do in C++ something like this: Lecture
I was at a lecture with Bjarne Stoustrup recently, he was talking about c++

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.