{"id":2419,"date":"2015-09-30T11:45:49","date_gmt":"2015-09-30T10:45:49","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=2419"},"modified":"2015-09-30T11:45:49","modified_gmt":"2015-09-30T10:45:49","slug":"c11-move-semantics-force-it","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=2419","title":{"rendered":"c++11 move semantics - force it"},"content":{"rendered":"<pre><code>\r\n\/\/ $ g++ --std=c++11 -Wall -Wextra focemove.cpp\r\n\/\/ $ .\/a.out\r\n\/\/ Yes using rvalue, move semantics\r\n\/\/ Yes using rvalue, move semantics\r\n\/\/ Oh no you forgot std::move somewhere. Deep copy occurs\r\n\/\/\r\n\/\/ After delete unwanted ctor\r\n\/\/ focemove.cpp: In function \u2018void helper(std::vector< int >&&)\u2019:\r\n\/\/ focemove.cpp:44:20: error: use of deleted function \u2018TestClass::TestClass(std::vector< int >&)\u2019\r\n\/\/ focemove.cpp:28:3: error: declared here\r\n\/\/\r\n\/\/ testet with 4.7.4 - 5.2.1\r\n\r\n#include < vector >\r\n#include < iostream > \r\n#include < utility >\r\n\r\nusing namespace std;\r\n\r\n\/\/ this class store biiiig data in a std::vector.\r\n\/\/ If we initialize it with a const reference ctor a deep copy occurs.\r\n\/\/ To prevent this, either dont proice a const reference ctor or better:\r\n\/\/ forbid it at all.\r\nclass TestClass {\r\npublic:\r\n  TestClass(vector< int > & other)\r\n    : vec(other)\r\n  {\r\n    cout << \"Oh no you forgot std::move somewhere. Deep copy occurs\\n\";\r\n  }\r\n\r\n\/\/ do we have to delete a const and non-const version here??\r\n\/\/   TestClass(const vector< int > & other) = delete;\r\n\/\/   TestClass(vector< int > & other) = delete;\r\n\r\n  TestClass(vector< int > && other)\r\n    : vec(other)\r\n  {\r\n    cout << \"Yes using rvalue, move semantics\\n\";\r\n  }\r\nprivate:\r\n  vector< int > vec;\r\n};\r\n\r\nvoid helper(vector< in t> && other) {\r\n  \/\/ is other a rvalue or lvalue? It depends on the caller\r\n\r\n  \/\/ here we forgot to use std::move(other)\r\n  \/\/ other is a named rvalue and thus no move semantics are used\r\n  TestClass a(other);\r\n\r\n  \/\/ comment in this to test after fix TestClass constructor\r\n\/\/   TestClass a(move(other));\r\n}\r\n\r\nint main() {\r\n  \/\/ this works all fine. All use move semantics\r\n  TestClass(vector< int >());    \/\/ using temporaries, unnamed variables\r\n  vector< int > other;\r\n  TestClass(std::move(other)); \/\/ named variable other is converted to an unnamed with std::move\r\n\r\n  \/\/ this call is okay\r\n  helper(std::move(other));\r\n\r\n  \/\/ this line triggers a error\r\n  \/\/ cannot bind \u2018std::vector< int >\u2019 lvalue to \u2018std::vector< int >&&\u2019\r\n  \/\/ helper(other);\r\n  return 0;\r\n}\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ $ g++ --std=c++11 -Wall -Wextra focemove.cpp \/\/ $ .\/a.out \/\/ Yes using rvalue, move semantics \/\/ Yes using rvalue, move semantics \/\/ Oh no you forgot std::move somewhere. Deep copy occurs \/\/ \/\/ After delete unwanted ctor \/\/ focemove.cpp: In function \u2018void helper(std::vector< int >&#038;&)\u2019: \/\/ focemove.cpp:44:20: error: use of deleted function \u2018TestClass::TestClass(std::vector< int [&hellip;]\n<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17],"class_list":["post-2419","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2419","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2419"}],"version-history":[{"count":2,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":2421,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/2419\/revisions\/2421"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}