Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

The std::forward template is usually for dependent types. Please read this questionthis question carefully to see whether it applies here. This is a difficult subject to master, so feel free to update your question with relevant details about your exact problem (using rvalue references for integers isn't terribly exciting...).

I believe your question is about the understanding of the basic properties of rvalue references. The rule of thumb to remember is:

  • whatever has a name is a lvalue (const or not).
  • whatever has no name is a rvalue.
  • Types with && bind to rvalues.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass& ! } 

then inside the body, x is a name, and therefore a l value. It really has type SomeClass&. You must use std::move to turn a SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { // Since `x` has a name here, it is a Lvalue. // Therefore it has type SomeClass&, what the signature doesn't indicate. // We thus have to explicitly turn it into a rvalue: foo(std::move(x)); } 

The std::forward template is usually for dependent types. Please read this question carefully to see whether it applies here. This is a difficult subject to master, so feel free to update your question with relevant details about your exact problem (using rvalue references for integers isn't terribly exciting...).

I believe your question is about the understanding of the basic properties of rvalue references. The rule of thumb to remember is:

  • whatever has a name is a lvalue (const or not).
  • whatever has no name is a rvalue.
  • Types with && bind to rvalues.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass& ! } 

then inside the body, x is a name, and therefore a l value. It really has type SomeClass&. You must use std::move to turn a SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { // Since `x` has a name here, it is a Lvalue. // Therefore it has type SomeClass&, what the signature doesn't indicate. // We thus have to explicitly turn it into a rvalue: foo(std::move(x)); } 

The std::forward template is usually for dependent types. Please read this question carefully to see whether it applies here. This is a difficult subject to master, so feel free to update your question with relevant details about your exact problem (using rvalue references for integers isn't terribly exciting...).

I believe your question is about the understanding of the basic properties of rvalue references. The rule of thumb to remember is:

  • whatever has a name is a lvalue (const or not).
  • whatever has no name is a rvalue.
  • Types with && bind to rvalues.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass& ! } 

then inside the body, x is a name, and therefore a l value. It really has type SomeClass&. You must use std::move to turn a SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { // Since `x` has a name here, it is a Lvalue. // Therefore it has type SomeClass&, what the signature doesn't indicate. // We thus have to explicitly turn it into a rvalue: foo(std::move(x)); } 
added 877 characters in body
Source Link
Alexandre C.
  • 57.4k
  • 13
  • 136
  • 200

The std::forward template is usually for function templatesdependent types. Please read this question carefully to see whether it applies here. This is a difficult subject to master, so feel free to update your question with relevant details about your exact problem (using rvalue references for integers isn't terribly exciting...).

I believe your question is about the understanding of the basic properties of rvalue references. The rule of thumb to remember is:

  • whatever has a name is a lvalue (const or not).
  • whatever has no name is a rvalue.
  • Types with && bind to rvalues.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass&. ! } 

So youthen inside the body, x is a name, and therefore a l value. It really has type SomeClass&. You must use std::move to turn a SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { // Since `x` has a name here, it is a Lvalue. // Therefore it has type SomeClass&, what the signature doesn't indicate. // We thus have to explicitly turn it into a rvalue: foo(std::move(x)); } 

std::forward is for function templates.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass&. } 

So you must use std::move to turn SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { foo(std::move(x)); } 

The std::forward template is usually for dependent types. Please read this question carefully to see whether it applies here. This is a difficult subject to master, so feel free to update your question with relevant details about your exact problem (using rvalue references for integers isn't terribly exciting...).

I believe your question is about the understanding of the basic properties of rvalue references. The rule of thumb to remember is:

  • whatever has a name is a lvalue (const or not).
  • whatever has no name is a rvalue.
  • Types with && bind to rvalues.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass& ! } 

then inside the body, x is a name, and therefore a l value. It really has type SomeClass&. You must use std::move to turn a SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { // Since `x` has a name here, it is a Lvalue. // Therefore it has type SomeClass&, what the signature doesn't indicate. // We thus have to explicitly turn it into a rvalue: foo(std::move(x)); } 
Source Link
Alexandre C.
  • 57.4k
  • 13
  • 136
  • 200

std::forward is for function templates.

If you have a function...

void foo(SomeClass&& x) { // ... then here x has type SomeClass&. } 

So you must use std::move to turn SomeClass& into SomeClass&&:

void bar(SomeClass&& x) { foo(std::move(x)); }