Why is my code printing rvalue 2 times instead of rvalue & lvalue?C++0x rvalue references - lvalues-rvalue bindingWhat are rvalues, lvalues, xvalues, glvalues, and prvalues?Classes, Rvalues and Rvalue ReferencesWould you ever mark a C++ RValue reference parameter as constProviding different implementations of a class depending on lvalue/rvalue when using expression templatesVariadic template class constructor with lvalues and rvaluesC++ Operator Overloading [ ] for lvalue and rvalueWhy rvalue reference binding to xvalue doesn't work in my code?When to prefer const lvalue reference over rvalue reference templatesUnderstanding perfect forwarding

Journal standards vs. personal standards

Why wasn't ASCII designed with a contiguous alphanumeric character order?

Would skyscrapers tip over if people fell sideways?

Word ending in "-ine" for rat-like

"I am [the / an] owner of a bookstore"?

Why will we fail creating a self sustaining off world colony?

How far can gerrymandering go?

Perform mirror symmetry transformation of 3D model (in OBJ)

Is it advisable to inform the CEO about his brother accessing his office?

English idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)

What prevents a US state from colonizing a smaller state?

Is leaving out prefixes like "rauf", "rüber", "rein" when describing movement considered a big mistake in spoken German?

Fast method to cut/shred glue stick into small pieces

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

I just started; should I accept a farewell lunch for a coworker I don't know?

Dynamic Sql Query - how to add an int to the code?

Why didn't Caesar move against Sextus Pompey immediately after Munda?

Copy group of files (Filename*) to backup (Filename*.bak)

Why did the Apple //e make a hideous noise if you inserted the disk upside down?

How did they film the Invisible Man being invisible in 1933?

Russian equivalents of 能骗就骗 (if you can cheat, then cheat)

A* pathfinding algorithm too slow

Where can I find my serialized Sitecore items?

Checkmate in 1 on a Tangled Board



Why is my code printing rvalue 2 times instead of rvalue & lvalue?


C++0x rvalue references - lvalues-rvalue bindingWhat are rvalues, lvalues, xvalues, glvalues, and prvalues?Classes, Rvalues and Rvalue ReferencesWould you ever mark a C++ RValue reference parameter as constProviding different implementations of a class depending on lvalue/rvalue when using expression templatesVariadic template class constructor with lvalues and rvaluesC++ Operator Overloading [ ] for lvalue and rvalueWhy rvalue reference binding to xvalue doesn't work in my code?When to prefer const lvalue reference over rvalue reference templatesUnderstanding perfect forwarding













9















So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2 instances of class on stack and to my surprise both of which are using the T&& overload.



#include <iostream>
#include <type_traits>
#include <utility>

template <class T> auto forward(T &&t)
if constexpr (std::is_lvalue_reference<T>::value)
return t;

return std::move(t);


template <class T> class Test
public:
Test(T &) std::cout << "lvalue" << std::endl; ;
Test(T &&) std::cout << "rvalue" << std::endl; ;
;

int main()
int x = 5;
Test<int> a(forward(3));
Test<int> b(forward(x));
return 0;



I tried using the original std::forward function and implementing it but both times it printed rvalue x2. What am I doing wrong?










share|improve this question



















  • 2





    auto is never a reference.

    – Evg
    Jun 21 at 15:42















9















So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2 instances of class on stack and to my surprise both of which are using the T&& overload.



#include <iostream>
#include <type_traits>
#include <utility>

template <class T> auto forward(T &&t)
if constexpr (std::is_lvalue_reference<T>::value)
return t;

return std::move(t);


template <class T> class Test
public:
Test(T &) std::cout << "lvalue" << std::endl; ;
Test(T &&) std::cout << "rvalue" << std::endl; ;
;

int main()
int x = 5;
Test<int> a(forward(3));
Test<int> b(forward(x));
return 0;



I tried using the original std::forward function and implementing it but both times it printed rvalue x2. What am I doing wrong?










share|improve this question



















  • 2





    auto is never a reference.

    – Evg
    Jun 21 at 15:42













9












9








9


1






So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2 instances of class on stack and to my surprise both of which are using the T&& overload.



#include <iostream>
#include <type_traits>
#include <utility>

template <class T> auto forward(T &&t)
if constexpr (std::is_lvalue_reference<T>::value)
return t;

return std::move(t);


template <class T> class Test
public:
Test(T &) std::cout << "lvalue" << std::endl; ;
Test(T &&) std::cout << "rvalue" << std::endl; ;
;

int main()
int x = 5;
Test<int> a(forward(3));
Test<int> b(forward(x));
return 0;



I tried using the original std::forward function and implementing it but both times it printed rvalue x2. What am I doing wrong?










share|improve this question
















So I wanted to practice the usage of std::forward and created a Test class with 2 constructors. 1 with T& and the other with T&& as overload. T& prints lvalue, and T&& prints rvalue so I know which one of the constructors is being used. I create 2 instances of class on stack and to my surprise both of which are using the T&& overload.



#include <iostream>
#include <type_traits>
#include <utility>

template <class T> auto forward(T &&t)
if constexpr (std::is_lvalue_reference<T>::value)
return t;

return std::move(t);


template <class T> class Test
public:
Test(T &) std::cout << "lvalue" << std::endl; ;
Test(T &&) std::cout << "rvalue" << std::endl; ;
;

int main()
int x = 5;
Test<int> a(forward(3));
Test<int> b(forward(x));
return 0;



I tried using the original std::forward function and implementing it but both times it printed rvalue x2. What am I doing wrong?







c++ forward rvalue lvalue stdmove






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 21 at 15:46









Evg

6,0573 gold badges20 silver badges43 bronze badges




6,0573 gold badges20 silver badges43 bronze badges










asked Jun 21 at 15:34









3l4x3l4x

483 bronze badges




483 bronze badges







  • 2





    auto is never a reference.

    – Evg
    Jun 21 at 15:42












  • 2





    auto is never a reference.

    – Evg
    Jun 21 at 15:42







2




2





auto is never a reference.

– Evg
Jun 21 at 15:42





auto is never a reference.

– Evg
Jun 21 at 15:42










1 Answer
1






active

oldest

votes


















14














Your problem stems from the return type of forward. You use auto as the return type which will not deduce a reference for you. That means when you do return, no matter which branch it returns from, you return by value which means you have a prvalue.



What you need is decltype(auto) so you return an rvalue or lvalue reference, depending on the return statement. Using



template <class T> decltype(auto) forward(T &&t) 
if constexpr (std::is_lvalue_reference<T>::value)
return t;
else
return std::move(t);



gives you the output:



rvalue
lvalue





share|improve this answer

























  • Isn't there such a thing as an auto&, or am I making up silliness?

    – Chipster
    Jun 21 at 15:46






  • 3





    @Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

    – NathanOliver
    Jun 21 at 15:47











  • thank you, problem solved

    – 3l4x
    Jun 21 at 15:49






  • 2





    @3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

    – NathanOliver
    Jun 21 at 15:51






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    Jun 21 at 16:30











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56706481%2fwhy-is-my-code-printing-rvalue-2-times-instead-of-rvalue-lvalue%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









14














Your problem stems from the return type of forward. You use auto as the return type which will not deduce a reference for you. That means when you do return, no matter which branch it returns from, you return by value which means you have a prvalue.



What you need is decltype(auto) so you return an rvalue or lvalue reference, depending on the return statement. Using



template <class T> decltype(auto) forward(T &&t) 
if constexpr (std::is_lvalue_reference<T>::value)
return t;
else
return std::move(t);



gives you the output:



rvalue
lvalue





share|improve this answer

























  • Isn't there such a thing as an auto&, or am I making up silliness?

    – Chipster
    Jun 21 at 15:46






  • 3





    @Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

    – NathanOliver
    Jun 21 at 15:47











  • thank you, problem solved

    – 3l4x
    Jun 21 at 15:49






  • 2





    @3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

    – NathanOliver
    Jun 21 at 15:51






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    Jun 21 at 16:30
















14














Your problem stems from the return type of forward. You use auto as the return type which will not deduce a reference for you. That means when you do return, no matter which branch it returns from, you return by value which means you have a prvalue.



What you need is decltype(auto) so you return an rvalue or lvalue reference, depending on the return statement. Using



template <class T> decltype(auto) forward(T &&t) 
if constexpr (std::is_lvalue_reference<T>::value)
return t;
else
return std::move(t);



gives you the output:



rvalue
lvalue





share|improve this answer

























  • Isn't there such a thing as an auto&, or am I making up silliness?

    – Chipster
    Jun 21 at 15:46






  • 3





    @Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

    – NathanOliver
    Jun 21 at 15:47











  • thank you, problem solved

    – 3l4x
    Jun 21 at 15:49






  • 2





    @3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

    – NathanOliver
    Jun 21 at 15:51






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    Jun 21 at 16:30














14












14








14







Your problem stems from the return type of forward. You use auto as the return type which will not deduce a reference for you. That means when you do return, no matter which branch it returns from, you return by value which means you have a prvalue.



What you need is decltype(auto) so you return an rvalue or lvalue reference, depending on the return statement. Using



template <class T> decltype(auto) forward(T &&t) 
if constexpr (std::is_lvalue_reference<T>::value)
return t;
else
return std::move(t);



gives you the output:



rvalue
lvalue





share|improve this answer















Your problem stems from the return type of forward. You use auto as the return type which will not deduce a reference for you. That means when you do return, no matter which branch it returns from, you return by value which means you have a prvalue.



What you need is decltype(auto) so you return an rvalue or lvalue reference, depending on the return statement. Using



template <class T> decltype(auto) forward(T &&t) 
if constexpr (std::is_lvalue_reference<T>::value)
return t;
else
return std::move(t);



gives you the output:



rvalue
lvalue






share|improve this answer














share|improve this answer



share|improve this answer








edited Jun 21 at 15:47

























answered Jun 21 at 15:43









NathanOliverNathanOliver

107k19 gold badges159 silver badges236 bronze badges




107k19 gold badges159 silver badges236 bronze badges












  • Isn't there such a thing as an auto&, or am I making up silliness?

    – Chipster
    Jun 21 at 15:46






  • 3





    @Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

    – NathanOliver
    Jun 21 at 15:47











  • thank you, problem solved

    – 3l4x
    Jun 21 at 15:49






  • 2





    @3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

    – NathanOliver
    Jun 21 at 15:51






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    Jun 21 at 16:30


















  • Isn't there such a thing as an auto&, or am I making up silliness?

    – Chipster
    Jun 21 at 15:46






  • 3





    @Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

    – NathanOliver
    Jun 21 at 15:47











  • thank you, problem solved

    – 3l4x
    Jun 21 at 15:49






  • 2





    @3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

    – NathanOliver
    Jun 21 at 15:51






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    Jun 21 at 16:30

















Isn't there such a thing as an auto&, or am I making up silliness?

– Chipster
Jun 21 at 15:46





Isn't there such a thing as an auto&, or am I making up silliness?

– Chipster
Jun 21 at 15:46




3




3





@Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

– NathanOliver
Jun 21 at 15:47





@Chipster There is, but that will always return an lvalue reference, which would not be what you want in a forwarding function.

– NathanOliver
Jun 21 at 15:47













thank you, problem solved

– 3l4x
Jun 21 at 15:49





thank you, problem solved

– 3l4x
Jun 21 at 15:49




2




2





@3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

– NathanOliver
Jun 21 at 15:51





@3l4x You'e welcome. Good first effort BTW. Generic code and forwarding can be pretty tough to get right.

– NathanOliver
Jun 21 at 15:51




1




1





@NathanOliver, even for compilers. ;)

– Evg
Jun 21 at 16:30






@NathanOliver, even for compilers. ;)

– Evg
Jun 21 at 16:30







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56706481%2fwhy-is-my-code-printing-rvalue-2-times-instead-of-rvalue-lvalue%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?