std move () : How this template function works?

Share the Article

The std::move( ) is a function template which perform static casts. The std::move( ) unconditionally casts its argument (an L-value or R-value) to an R-value. This template function takes a reference to an object (using a universal reference parameter) and it returns a R-value reference to the same object.

Example,

int x = 9; int && y = std::move(x); //Move taking L-value argument //& returning R-value int && z = std::move(4); //Move taking R-value argument //& returning R-value auto up1 = std::make_unique<int>(); std::unique_ptr<int> up2 = std::move(up1); //Move taking unique_ptr //& returning R-value of data

std::move does not move anything but it simply cast its argument to an R-value. Please note that everything happens only in compile time.

The simplest implementation of move is as follows:

template <typename T> typename std::remove_reference<T>::type&& move(T&& param) { using ReturnType = typename std::remove_reference<T>::type&&; return static_cast<ReturnType>(param); }
  • The parameter type is T&& (universal reference), which means it accepts both L-value and R-value arguments.
  • The “&&” part of the function’s return type means that std::move returns only an r-value reference.
  • The function uses typename because, we are using a datatype inside a template class T (dependent type)
using ReturnType = typename std::remove_reference<T>::type&&;

The funciton uses remove_reference because this ensures that whatever is the calling type (L-value or R-value), the return type always correspond to R-value of basic type.

The std::remove_reference template class is part of C++ library, however, we can easily implement this in following way:

template <class T> struct remove_reference { typedef T type; }; template <class T> struct remove_reference<const T> { typedef const T type; }; template <class T> struct remove_reference<T&> { typedef T type; }; template <class T> struct remove_reference<const T&> { typedef const T type; }; template <class T> struct remove_reference<T&&> { typedef T type; }; template <class T> struct remove_reference<const T&&> { typedef const T type; };

Code Example: Demonstration of local implementation of move function

#include <iostream> using namespace std; template <typename T> typename std::remove_reference<T>::type&& my_move(T&& param) { using ReturnType = typename std::remove_reference<T>::type&&; return static_cast<ReturnType>(param); } void check(int& a) //L-value version { cout << "L-value check() called" << endl; } void check(int&& a) //R-value version { cout << "R-value check() called" << endl; } int main() { check(5); //Calls check(&&) int w; check(w); //Calls check(&) check(my_move(w)); //Calls check(&&) return 0; }

The output is:

R-value check() called L-value check() called R-value check() called

Main Funda: std::move( ) does not move anything, it simply cast anything to its correspoding R-value at compile time

Related Topics:

 What are the drawbacks of using enum ?
Which member functions are generated by compiler in class?
How to stop compiler from generating special member functions?
Compiler Generated Destructor is always non-virtual
How to make a class object un-copyable?
Why virtual functions should not be called in constructor & destructor ?
Explaining C++ casts
How pointer to class members are different ?
How std::forward( ) works?
What is reference collapsing?
How std::move() function works?
How delete keyword can be used to filter polymorphism
Rule of Three

Share the Article

4,242 thoughts on “std move () : How this template function works?

  1. Good post. I learn something new and challenging on sites I stumbleupon
    Hi, I do think this is an excellent blog. I stumbledupon it 😉 I am going to
    “강남오피”
    come back once again since I book marked it. Money and freedom is the
    best way to change, may you be rich and continue to help others.

  2. Thanks for the sensible critique. Me & my neighbor were just preparing to do a little research about this. We got a grab a book from our local library but I think I learned more clear from this post. I am very glad to see such excellent info being shared freely out there.

  3. After going through many blog posts on your site, I really appreciate your way of blogging. I’m adding it to my list of bookmarked sites, you gotta get on with your writing. I believe that you already have a huge readership!

  4. Undeniably believe that which you stated. Your favourite reason appeared to be on the internet the simplest factor to keep in mind
    of. I say to you, I definitely get irked at the same
    time as people consider concerns that they plainly do not understand about.
    You controlled to hit the nail upon the highest as smartly as defined out the whole thing
    with no need side-effects , other people can take
    a signal. Will likely be back to get more. Thanks

    Also visit my webpage :: Dulcie Pacheco

  5. เข้าเล่นเกมสล็อต กับทางเว็บไซต์พวกเรา มั่นอกมั่นใจ ได้เลยว่า เว็บไซต์สล็อตทุกค่าย ไม่มีการฉ้อโกง แจกเครดิตฟรี 100 เกิดขึ้นอย่างแน่แท้ พร้อมสามารถ เข้าเล่นเกมสล็อต บนหน้าเว็บ รวมสล็อต ได้เลยในทันที อยู่ที่ใด ก็เล่นได้ทุกแห่ง ตลอดระยะเวลา ทางเว็บไซต์พวกเรา ไม่ว่างระบุ

  6. เกมสล็อต ทั้งปวง ล้วนแล้วแต่เป็น เกมสล็อตมาตรฐาน ระดับสากล แนวหน้า ของทวีปเอเชีย ที่มีอุปกรณ์ ฝาก20รับ100 ตรวจหา โปรแกรมการฉ้อโกง สล็อตเครดิตฟรี รวมทุกค่าย รวมทุกเว็บไซต์ ให้ทุกคน เชื่อมั่นทุกการพนัน สล็อตทุกค่าย

  7. ต้องการทำรายการ เบิกเงิน ก็สามารถ ทำรายการ ได้เลยโดยทันที ทางเว็บไซต์พวกเรา เครดิตฟรี กดรับเอง รวมสล็อต ไม่มีพันธะ พร้อมมอบ อิสระทางด้านการเงิน ให้ทุกคน อย่างมาก ตลอดการเข้าเล่นเกม

  8. สล็อตเครดิตฟรี รวมทุกค่าย รวมทุกเว็บไซต์ ก็สามารถ เข้าเล่นเกมได้ สล็อตทุกค่าย ถ้าเกิดท่าน 20รับ100 เข้าเล่นเกมสล็อต ของทางเว็บไซต์พวกเรา เว็บไซต์สล็อตทุกค่าย ได้รับเงินรางวัล โบนัส เยอะๆ

  9. สล็อต เว็บไซต์เดียวจบ ทุกค่าย แจกโบนัสแบบไม่ยั้ง ซึ่งรายนามค่ายเกม UFACASH ที่พวกเราต้องการพรีเซ็นท์นี้ นับว่าเป็นค่ายเกมสล็อตออนไลน์ที่ออกจะเป็นเกมที่เล่นง่าย ยอดฮิตอยู่ในเวลานี้

  10. ถ้าผู้เล่นท่านใดต้องการเล่นเกมสล็อตแม้กระนั้นยังไม่ทราบว่า เล่นสล็อตเกมไหนดี ขอชี้แนะ สล็อต BETFLIX เว็บไซต์เดียวจบ ทุกค่าย ค่ายไหนดี พวกเราขอบอกเลยว่ามีมากไม่น้อยเลยทีเดียวเลยที่เล่นแล้ว

  11. ถ้าเกิดท่าน เข้าเล่นเกมสล็อต ของทางเว็บไซต์พวกเรา เว็บไซต์สล็อตทุกค่าย ได้รับเงินรางวัล 10รับ100 โบนัส จำนวนมาก ต้องการทำรายการ เบิกเงิน ก็สามารถ ทำรายการ ได้เลยในทันที ทางเว็บไซต์พวกเรา

  12. สล็อตทุกค่าย ไม่ว่าท่าน ต้องการทำรายการ เงินสำหรับในการเล่นเกม ได้แบบตามใจ สล็อตเว็บตรง ท่านไม่มีความจำเป็นที่ต้องมีทุนสูง สล็อตเครดิตฟรี รวมทุกค่าย รวมทุกเว็บไซต์ ก็สามารถ เข้าเล่นเกมได้ สล็อตทุกค่าย

  13. สล็อตทุกค่าย ฝากถอน ไม่มีอย่างต่ำ เบทถูก ที่สุด เมื่อท่าน สมัครเข้ามา เป็นส่วนหนึ่งส่วนใด ของทางเว็บไซต์พวกเรา 50รับ100 เว็บไซต์รวมสล็อตทุกค่าย ฝากถอน ไม่มี อย่างน้อย ทุกคนสามารถ เลือกทำรายการ ทางด้านการเงิน ได้แบบตามใจ ทางเว็บไซต์พวกเรา ไม่มีกฎเกณฑ์

  14. ฝากถอน ไม่มี อย่างน้อย ไม่ว่าท่าน เข้าเล่นเกมสล็อต กับทางเว็บไซต์พวกเรา เวลาใด ก็มีเงินรางวัล แจกให้ทุกคน 50รับ100 ได้ลุ้นรับอย่างแน่แท้ สล็อต เว็บไซต์เดียวจบ ทุกค่าย ท่านไม่มีความจำเป็น ควรจะมีนั่งรอเวลา สำหรับในการเล่นเกม

  15. เกมสล็อตเล่นง่าย ได้เงินจริง พร้อมสามารถ ทำเงิน สร้างผลกำไร ให้ทุกคน ได้อย่างดีเยี่ยม สล็อต เว็บไซต์เดียวจบ เครดิตฟรี ทุกค่าย แจกเงินรางวัล โบนัส แจ็คพอต สปินฟรี ให้ทุกคน ได้ลุ้นรับตลอดทุกเมื่อ สำหรับในการเล่นเกม เว็บไซต์รวมสล็อตทุกค่าย

  16. Appreciation for taking the time to discuss this topic, I would love to discover
    more on this topic. If viable, as you gain expertise, would you object to updating
    the website with further information?“밤의전쟁” It is tremendously beneficial for me.

  17. Whats up very nice blog!! Guy .. Beautiful .. Amazing .. I’ll bookmark your blog and take the feeds additionally?KI am glad to seek out so many helpful info right here in the put up, we need work out extra techniques in this regard, thank you for sharing. . . . . .

  18. I’m writing on this topic these days, totosite, but I have stopped writing because there is no reference material. Then I accidentally found your article. I can refer to a variety of materials, so I think the work I was preparing will work! Thank you for your efforts.

  19. all already know how much iwant to give a subscribe or
    a follow for this. Let me tell you my ways on really amazing“오피뷰”stuff and if you want to have a glance? I will share info about how to make passive income check and follow me
    bros!

  20. The assignment submission period was over and I was nervous, baccaratsite and I am very happy to see your post just in time and it was a great help. Thank you ! Leave your blog address below. Please visit me anytime.

  21. When I read an article on this topic, baccaratsite the first thought was profound and difficult, and I wondered if others could understand.. My site has a discussion board for articles and photos similar to this topic. Could you please visit me when you have time to discuss this topic?

  22. 비아그라
    비아그라 구매
    비아그라 구입
    비아그라 퀵배송
    비아그라 퀵배송
    비아그라 구매
    비아그라 구입
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매

  23. 비아그라 구매
    비아그라 구입
    비아그라 퀵배송
    비아그라 퀵배송
    비아그라 구매
    비아그라 구입
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매
    비아그라 구매

  24. This research was supported by a British Heart Foundation Programme Grant to DJB and LDR RG 17 11 33042, a Wellcome Investigator Award to DJB 110044 Z 15 Z, and a British Heart Foundation PhD Studentship to ELE zithromax coupon

  25. I simply could not go away your website before suggesting that I actually loved the standard info an individual provide on your guests? Is going to be again regularly to inspect new posts.

  26. 경마솔루션제작,임대,경마사이트 제작/임대,경마프로그램 제작/임대,경마사이트 분양,경마프로그램 분양,최신형경마솔루션,최신형경마프로그램 및경마솔루션임대 대한민국1위 경마솔루션업체입니다.

  27. 【경마솔루션】경마솔루션제작,경마사이트제작 ,최신형경마솔루션,경마프로그램,경마API를【월200만원】으로 대한민국1위 가성비 좋은 경마솔루션업체입니다

  28. Hello, i think that i saw you visited my weblog thus i came to “return the favor”.I’m trying to find things to improve my web site!I suppose its ok to use a few of your ideas!!

  29. What a pleasure you’ve covered the whole thing in this post, thank you so much for your submission, very useful. You should keep writing. I am confident that you already have a huge readership!

  30. 【경마솔루션】경마솔루션제작|임대|분양/경마사이트제작|분양|임대사이트 fox공팔공.콤 【텔레@vm8282】【월150만원】으로 대한민국1위 가성비 좋은 경마솔루션업체입니다

  31. This time I’m reading this great educational piece, is your theme custom made or downloaded from somewhere? A design like yours can make mine with just a few simple tweaks
    Blogs stand out. Please let me know where you got your theme from.

  32. This time I’m reading this great educational piece, is your theme custom made or downloaded from somewhere? A design like yours can make mine with just a few simple tweaks
    Blogs stand out. Please let me know where you got your theme from.

  33. ทาง เข้า slotxoบ่อยครั้งที่ท่าน ประสบเจอกับปัญหา ในเรื่องของ หนทางการเข้าเล่น pgslot ผ่านหน้าเว็บ xo ที่ค้นหาทั่วๆไป ตามอินเตอร์เน็ต แล้วพบว่า เข้าได้บ้าง มิได้บ้าง

  34. The idea of losing hair is very difficult to deal with for many men, but we believe that accepting symptoms of low testos terone in order to retain a receding or thinning hairline doesn t make sense viagra and cialis online To make sure you can safely take Furosemida Winthrop, tell your doctor if you have any of these other conditions

  35. For abnormal heartbeat caused by a pacemaker, 2 grams of magnesium sulfate in 10 mL of solution has been given by IV over 1 10 minutes, followed by 5 10 grams of magnesium sulfate in 250 500 mL of solution over 5 hours can i buy cialis without a prescription 3j, 6d, neurons were loaded with the pipette solution see below containing 5 mM biocytin for 20 min

  36. 우리 비아스토어는 정품비아그라 구입,정품비아그라 구매,정품비아그라 판매,비아그라 약국,비아그라 구입방법 온라인 국내 최저가격으로 처방없이 구매.편한 상담,끝내주는 서비스,고객님 정보 보호,깔끔한 거래로 항상 고객만족도 1위,재구매율 1위 사이트입니다.

  37. Pingback: 2thomson
  38. I’d like to thank you for the efforts you’ve put in penning this blog. I’m hoping to view the same high-grade content by you later on as well. In fact, your creative writing abilities has encouraged me to get my own blog now 😉

  39. Thanks for all your valuable efforts on this site. Debby really loves carrying out internet research and it is obvious why. I know all concerning the dynamic tactic you offer invaluable tips and hints on your website and even foster contribution from some other people on this situation and our girl is really starting to learn a whole lot. Enjoy the remaining portion of the new year. You are performing a remarkable job.

  40. Thanks for all your valuable efforts on this site. Debby really loves carrying out internet research and it is obvious why. I know all concerning the dynamic tactic you offer invaluable tips and hints on your website and even foster contribution from some other people on this situation and our girl is really starting to learn a whole lot. Enjoy the remaining portion of the new year. You are performing a remarkable job.

  41. 경마사이트는 온라인경마사이트,실시간경마사이트,인터넷경마사이트,안전한경마사이트,사설경마사이트,한국경마,일본경마,스크린경마 등 먹튀검증 업계 1위 경마사이트

  42. 우리 업체는 인증된 업체입니다. 정품을 저렴한 가격에 비밀포장, 택배비 무료, 당일 배송을 원칙으로 합니다. 오후 4시까지 입금이 확인되면 빠르게 받아보실 수 있습니다. 그 이후에도 확인이 되면 당일배송이 가능합니다. 그 외에 다른 문의사항 있으시면 24시간 친절한 상담원이 답변해드리고 있습니다.
    저희가 판매하는 제품은 모두 정품입니다. 인증되지 않은 제품은 판매하지 않습니다.
    오후 4시까지 입금이 확인되면 당일배송이 가능합니다. 프라이버시를 위한 불투명한 3중 포장으로 배송해드리고 있습니다. 할인된 금액과 이벤트로 가성비있는 제품을 받아보세요.
    7일 24시간 365일 연중무휴 대기중입니다.

  43. 100% 정품 판매 업체
    VIAGRA – CIALIS
    처방전 필요없는 비아그라 | 처방전 없이 시알리스 구매

    비아그라 처방전 없이 살 수 있나요? 네! 가능합니다.
    비아그라와 시알리스를 처방전 없이 구매하세요. 100% 정품 판매.

    발기부전 치료를 위한 대표적인 2가지 발기부전 치료 약.

    안전하고 비밀을 보장한 채 빠르고 신속한

    비아그라 퀵 배송 전문!

    1 : 1 카카오톡 문의 가능. 신속한 답변.

    다양한 상품 1 + 1 이벤트!

  44. 비아그라 구입비아그라구매 비아그라판매 비아그라후기 비아그라처방 비아그라판매처 비아그라가격 비아그라구입처 비아그라정품 비아그라효과 비아그파는곳 비아그라복용법 비아그라부작용 비아그라종류 비아그라사이트 비아그라구입방법 비아그라팝니다비아그라 구입비아그라구매 비아그라판매 비아그라후기 비아그라처방 비아그라판매처 비아그라가격 비아그라구입처 비아그라정품 비아그라효과 비아그파는곳 비아그라복용법 비아그라부작용 비아그라종류 비아그라사이트 비아그라구입방법 비아그라팝니다

  45. 비아그라 파는곳,온라인 약국 비아그라,비아그라 구매,비아그라 퀵배송,비아그라 구입처 모든 상품100% 정품임을 보장하며 모든 배송은 비밀배송 퀵배송 가능합니다

  46. 안전한 비아그라 퀵배송은 소중한 고객님의 프라이버시를 위한 비밀보장 및 배송해드립니다. 당일 배송.오후 2시 이전 입금시 100%당일 무료 배송 원칙을 합니다.

  47. 비아그라파는곳,비아그라 파는곳,비아그라 구입,비아그라 퀵배송, 비아그라구매, 시알리스 파는​곳,,비아그라 구입방법,온라인 비아그라판매스토어,비아그라 구입,발기부전치료제,비아그라 처방전없이구입,비아그라 판매사이트,비아그라,시알리스,레비트라,팔팔정,온라인 비아그라 구매 구입 사이트 비아스토어 입니다.
    100%정품 국내 최저가로 비아스토어에서 만나 보세요!

  48. 비아그라는 발기부전으로 어려움을 겪는 남성들에게 큰 힘이 되어주는 약품이다. 비아그라는 주로 발기부전 치료제로 이용되지만, 다른 용도로 이용하는 경우도 있다. 예를 들어, 비아그라는 폐고혈압을 치료하기 위한 목적으로도 이용된다. 또한 고산병 치료제로도 이용할 수 도 있다.

  49. 비아그라는 발기부전의 대표적인 약이지만 복용하는 사람의 건강상태와 유전적 요인등으로 작은 부작용들이 발생할 수 있기 때문에 의사와 상담한 후 적정량의 비아그라를 복용하는 것이 바람직하다

  50. 요요약국에서는 처방전 없이 저렴한 가격으로 100%정품을 구매를 할 수가 있습니다. 비아그라 구입을 원한다면 요요약국으로 오세요. 쉽게 100%정품을 구매 할수가 있습니다. 초보자로도 어렵지 않게 비아그라 구입을 할수가 있습니다.

  51. 먹튀검증 검증 업계 1위 경마사이트만 소개합니다
    안전하고 경마사이트 에서 실시간경마 를 즐기실 수 있습니다.
    인터넷이 있는곳이라면 어디서든 실시간경마를 영상으로 ​편하게 베팅하세요.

  52. 모바­일경마의 진수 최적화된 환경에서

    어디서든 간편하게 경마사이트 를 이용하세요.

    검빛 경마에서도 검증이 완료된 안전한 인터넷경마 만을 추천합니다.

    온라인경마는 반드시 믿을 수 있는곳에서 이용하여야합니다.

    일본경마사이트 까지 제공하고 있습니다.

  53. 경마사이트는 온라인경마사이트,실시간경마사이트,인터넷경마사이트,안전한경마사이트,먹튀검증 경마사이트,사설경마사이트,한국경마,일본경마,스크린경마 등 먹튀검증 업계 1위 경마사이트만 추천하며 전세계 경마사이트 관련 정보를 공유합니다.다양한 이벤트들이 제공되고 있으며 먹튀검증 경마사이트만을 추천합니다.

  54. 모바­일경마의 진수 최적화된 환경에서

    어디서든 간편하게 경마사이트 를 이용하세요.

    검빛 경마에서도 검증이 완료된 안전한 인터넷경마 만을 추천합니다.

    온라인경마는 반드시 믿을 수 있는곳에서 이용하여야합니다.

    일본경마사이트 까지 제공하고 있습니다.

  55. Thanks for the guidelines shared on your own blog. Another thing I would like to express is that losing weight is not all about going on a fad diet and trying to shed as much weight as you can in a couple of weeks. The most effective way to lose weight naturally is by using it slowly and following some basic points which can enable you to make the most through your attempt to lose fat. You may realize and be following most of these tips, but reinforcing information never does any damage.

  56. It is perfect time to make some plans for the future and it is time
    to be happy. I’ve read this post and if I could I desire to suggest
    you few interesting things or tips. Perhaps you could write next articles referring to this article.
    I want to read more things about it!