SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
Where	to	type	std::move?
Andrey	Upadyshev
Licensed	under	a	CC	BY-SA	4.0	License.	 Version	2016-04-26-1
The	Problem
void foo(Object && object)
{
// Should we apply std::move
// to the object member?
consume( std::move(object.member) );
// or to the object itself?
consume( std::move(object).member );
}
Note	that	it’s	all	related	to	std::forward as	well.
What	the	heck,	is	there	a	difference?
It	is.	Sometimes.
std::move(object.member)
• Means:	cast	a	result	of	the	object	member	access	to	a	[cv-qualified] rvalue.
• The	result	is	always an	rvalue.
std::move(object).member
• Means:	cast	the	object	to	a	[cv-qualified] rvalue then	access	its	member.
• If	member	is	neither	a	static	member	nor	a	reference	the	result	is	an	rvalue.
• Otherwise the	result	is	lvalue.
Why	should	I	care?
std::move(object.member) can	accidentally	move	from	a	shared	
object	if	applied	to	a	reference	or	a	static	member:
class Object {
Member& member;
…
};
...
std::move(object.member)
std::move(object).member never	does	so.
Think	about	generic	code	or	changing	a	member	type	as	a	result	of	
refactoring.
Generic	code	example
Somewhere	in	the	heart	of	the	generic	code:
consume(std::move(std::get<i>(tuple))...);
No	idea,	what’s	in	the	tuple.	It	can	be	a	value	or	a	reference.	When	
accidental	move	from	a	shared	object	is	happened	it	will	be	a	disaster.
This	approach	is	safe	(or	at	least	safer):
consume(std::get<i>(std::move(tuple))...);
Sometimes	it	may	be	really	different
Slice	from	a	tuple.	Member	that	are	rvalues are	moved:
auto tuple = hana::make_tuple("abc"s, "def"s,
"ghi"s, "jkl"s);
consume(hana::slice_c<0, 2>(std::move(tuple)));
consume(hana::slice_c<2, 4>(std::move(tuple)));
Applying	std::move at	the	last	step	is	useless,	members	are	already	copied	inside	
slice:
consume(std::move(hana::slice_c<0, 2>(tuple))); // copy
Another	example	where	behavior	may	differ	is	ref-qualified	methods.
Where	to	type	what?
• std::move(object.member) to	unconditionally	move	from	a	
member.
• std::move(object).member to	tell	that	the	object	is	actually	an	
rvalue then	only	the	safe	things	can	happen	J
Links
1. Andrey	Upadyshev,	What's	the	difference	between	std::move(object.member)	and	
std::move(object).member?
http://oliora.github.io/2016/02/12/where-to-put-std-move.html
2. Louis	Dionne,	A	tentative	notion	of	move-independence
http://ldionne.com/2016/02/17/a-tentative-notion-of-move-independence/
3. Reddit thread
https://www.reddit.com/r/cpp/comments/45w3fs/whats_the_difference_between_stdmoveo
bjectmember/
4. The	C++	Standard:	Class	Member	Access [expr.ref-4]
http://eel.is/c++draft/expr.ref#4

Más contenido relacionado

La actualidad más candente

JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
yoavrubin
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1
guest739536
 

La actualidad más candente (20)

Language tour of dart
Language tour of dartLanguage tour of dart
Language tour of dart
 
Regular types in C++
Regular types in C++Regular types in C++
Regular types in C++
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming Language
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 
Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画Ruby 3の型解析に向けた計画
Ruby 3の型解析に向けた計画
 
Protocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That WayProtocols with Associated Types, and How They Got That Way
Protocols with Associated Types, and How They Got That Way
 
Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3Type Profiler: Ambitious Type Inference for Ruby 3
Type Profiler: Ambitious Type Inference for Ruby 3
 
Template classes and ROS messages
Template classes and ROS messagesTemplate classes and ROS messages
Template classes and ROS messages
 
Optionals by Matt Faluotico
Optionals by Matt FaluoticoOptionals by Matt Faluotico
Optionals by Matt Faluotico
 
(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings(4) cpp automatic arrays_pointers_c-strings
(4) cpp automatic arrays_pointers_c-strings
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Swift, a quick overview
Swift, a quick overviewSwift, a quick overview
Swift, a quick overview
 
Lec12-CS110 Computational Engineering
Lec12-CS110 Computational EngineeringLec12-CS110 Computational Engineering
Lec12-CS110 Computational Engineering
 
JavaScript - Programming Languages course
JavaScript - Programming Languages course JavaScript - Programming Languages course
JavaScript - Programming Languages course
 
C traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmersC traps and pitfalls for C++ programmers
C traps and pitfalls for C++ programmers
 
Web application architecture
Web application architectureWeb application architecture
Web application architecture
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 
C++ to java
C++ to javaC++ to java
C++ to java
 
Start with swift
Start with swiftStart with swift
Start with swift
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1
 

Similar a Where to type_std_move?

OOPs_Lecture 09_Argument Passing, static members.pptx
OOPs_Lecture 09_Argument Passing, static members.pptxOOPs_Lecture 09_Argument Passing, static members.pptx
OOPs_Lecture 09_Argument Passing, static members.pptx
NAYEEMBASHA12
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
DigitalDsms
 
C Sharp Jn (5)
C Sharp Jn (5)C Sharp Jn (5)
C Sharp Jn (5)
jahanullah
 
C Sharp Jn (5)
C Sharp Jn (5)C Sharp Jn (5)
C Sharp Jn (5)
jahanullah
 

Similar a Where to type_std_move? (20)

OOPs_Lecture 09_Argument Passing, static members.pptx
OOPs_Lecture 09_Argument Passing, static members.pptxOOPs_Lecture 09_Argument Passing, static members.pptx
OOPs_Lecture 09_Argument Passing, static members.pptx
 
Sep 15
Sep 15Sep 15
Sep 15
 
Sep 15
Sep 15Sep 15
Sep 15
 
Core java Basics
Core java BasicsCore java Basics
Core java Basics
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx7. Pointers and Virtual functions final -3.pptx
7. Pointers and Virtual functions final -3.pptx
 
Chapter 8 java
Chapter 8 javaChapter 8 java
Chapter 8 java
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
Java Basics Presentation
Java Basics PresentationJava Basics Presentation
Java Basics Presentation
 
Introduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book NotesIntroduction to C++ Class & Objects. Book Notes
Introduction to C++ Class & Objects. Book Notes
 
Unit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdfUnit 2 Part 1 Constructors.pdf
Unit 2 Part 1 Constructors.pdf
 
Pooja Sharma , BCA Third Year
Pooja Sharma , BCA Third YearPooja Sharma , BCA Third Year
Pooja Sharma , BCA Third Year
 
Liberated APIs in ClojureLand - Paris Clojure User Group
Liberated APIs in ClojureLand - Paris Clojure User GroupLiberated APIs in ClojureLand - Paris Clojure User Group
Liberated APIs in ClojureLand - Paris Clojure User Group
 
C Sharp Jn (5)
C Sharp Jn (5)C Sharp Jn (5)
C Sharp Jn (5)
 
C Sharp Jn (5)
C Sharp Jn (5)C Sharp Jn (5)
C Sharp Jn (5)
 
Classes2
Classes2Classes2
Classes2
 
Static Members-Java.pptx
Static Members-Java.pptxStatic Members-Java.pptx
Static Members-Java.pptx
 
Generic Objects - Bill Wei - ManageIQ Design Summit 2016
Generic Objects - Bill Wei - ManageIQ Design Summit 2016Generic Objects - Bill Wei - ManageIQ Design Summit 2016
Generic Objects - Bill Wei - ManageIQ Design Summit 2016
 
WPF Concepts
WPF ConceptsWPF Concepts
WPF Concepts
 
Introduction to java programming
Introduction to java programmingIntroduction to java programming
Introduction to java programming
 

Último

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Último (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 18, Noida Call girls :8448380779 Model Escorts | 100% verified
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 

Where to type_std_move?