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

First-year PhD giving a talk among well-established researchers in the field

Did Karl Marx ever use any example that involved cotton and dollars to illustrate the way capital and surplus value were generated?

What do you call a weak person's act of taking on bigger opponents?

Is adding a new player (or players) a DM decision, or a group decision?

Inverse-quotes-quine

Does the posterior necessarily follow the same conditional dependence structure as the prior?

What is the difference between and Embedding Layer and an Autoencoder?

Is this one of the engines from the 9/11 aircraft?

Do flight schools typically have dress codes or expectations?

Should my manager be aware of private LinkedIn approaches I receive? How to politely have this happen?

Should I tell my insurance company I'm making payments on my new car?

How to get cool night-vision without lame drawbacks?

What are the benefits of using the X Card safety tool in comparison to plain communication?

MH370 blackbox - is it still possible to retrieve data from it?

Abel-Jacobi map on symmetric product of genus 4 curve

In the Marvel universe, can a human have a baby with any non-human?

What happens when I sacrifice a creature when my Teysa Karlov is on the battlefield?

STM Microcontroller burns every time

Is my Rep in Stack-Exchange Form?

Is it damaging to turn off a small fridge for two days every week?

What reason would an alien civilization have for building a Dyson Sphere (or Swarm) if cheap Nuclear fusion is available?

Are there any vegetarian astronauts?

Apply brace expansion in "reverse order"

Why would people reject a god's purely beneficial blessing?



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






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








7















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









New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 2





    auto is never a reference.

    – Evg
    9 hours ago

















7















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









New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.














  • 2





    auto is never a reference.

    – Evg
    9 hours ago













7












7








7








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









New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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









New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 9 hours ago









Evg

5,6883 gold badges19 silver badges42 bronze badges




5,6883 gold badges19 silver badges42 bronze badges






New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 9 hours ago









3l4x3l4x

383 bronze badges




383 bronze badges




New contributor



3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




3l4x is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









  • 2





    auto is never a reference.

    – Evg
    9 hours ago












  • 2





    auto is never a reference.

    – Evg
    9 hours ago







2




2





auto is never a reference.

– Evg
9 hours ago





auto is never a reference.

– Evg
9 hours ago












1 Answer
1






active

oldest

votes


















12














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
    9 hours ago






  • 3





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

    – NathanOliver
    9 hours ago











  • thank you, problem solved

    – 3l4x
    9 hours ago






  • 2





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

    – NathanOliver
    9 hours ago






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    8 hours ago














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
);



);






3l4x is a new contributor. Be nice, and check out our Code of Conduct.









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









12














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
    9 hours ago






  • 3





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

    – NathanOliver
    9 hours ago











  • thank you, problem solved

    – 3l4x
    9 hours ago






  • 2





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

    – NathanOliver
    9 hours ago






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    8 hours ago
















12














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
    9 hours ago






  • 3





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

    – NathanOliver
    9 hours ago











  • thank you, problem solved

    – 3l4x
    9 hours ago






  • 2





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

    – NathanOliver
    9 hours ago






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    8 hours ago














12












12








12







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 9 hours ago

























answered 9 hours ago









NathanOliverNathanOliver

106k19 gold badges157 silver badges233 bronze badges




106k19 gold badges157 silver badges233 bronze badges












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

    – Chipster
    9 hours ago






  • 3





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

    – NathanOliver
    9 hours ago











  • thank you, problem solved

    – 3l4x
    9 hours ago






  • 2





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

    – NathanOliver
    9 hours ago






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    8 hours ago


















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

    – Chipster
    9 hours ago






  • 3





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

    – NathanOliver
    9 hours ago











  • thank you, problem solved

    – 3l4x
    9 hours ago






  • 2





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

    – NathanOliver
    9 hours ago






  • 1





    @NathanOliver, even for compilers. ;)

    – Evg
    8 hours ago

















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

– Chipster
9 hours ago





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

– Chipster
9 hours ago




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
9 hours ago





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

– NathanOliver
9 hours ago













thank you, problem solved

– 3l4x
9 hours ago





thank you, problem solved

– 3l4x
9 hours ago




2




2





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

– NathanOliver
9 hours ago





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

– NathanOliver
9 hours ago




1




1





@NathanOliver, even for compilers. ;)

– Evg
8 hours ago






@NathanOliver, even for compilers. ;)

– Evg
8 hours ago













3l4x is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















3l4x is a new contributor. Be nice, and check out our Code of Conduct.












3l4x is a new contributor. Be nice, and check out our Code of Conduct.











3l4x is a new contributor. Be nice, and check out our Code of Conduct.














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

Sahara Skak | Bilen | Luke uk diar | NawigatsjuunCommonskategorii: SaharaWikivoyage raisfeerer: Sahara26° N, 13° O

The fall designs the understood secretary. Looking glass Science Shock Discovery Hot Everybody Loves Raymond Smile 곳 서비스 성실하다 Defas Kaloolon Definition: To combine or impregnate with sulphur or any of its compounds as to sulphurize caoutchouc in vulcanizing Flame colored Reason Useful Thin Help 갖다 유명하다 낙엽 장례식 Country Iron Definition: A fencer a gladiator one who exhibits his skill in the use of the sword Definition: The American black throated bunting Spiza Americana Nostalgic Needy Method to my madness 시키다 평가되다 전부 소설가 우아하다 Argument Tin Feeling Representative Gym Music Gaur Chicken 일쑤 코치 편 학생증 The harbor values the sugar. Vasagle Yammoe Enstatite Definition: Capable of being limited Road Neighborly Five Refer Built Kangaroo 비비다 Degree Release Bargain Horse 하루 형님 유교 석 동부 괴롭히다 경제력

19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу