SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Downloaden Sie, um offline zu lesen
Everything You Ever
Wanted To Know About
Move Semantics
Howard Hinnant
Senior Software Engineer
Ripple Labs
!
April 12, 2014
(and then some)
• The genesis of move semantics
• Special member functions
• Introduction to the special move members
Outline
How Did Move Semantics Get Started?
• It was all about optimizing std::vector<T>.
• And everything else just rode along on its coattails.
vector
What is std::vector?
• Anatomy of a vector (simplified)
vector’s data
begin() end() capacity()
How does a vector copy?
vector
vector’s data
vector
vector’s data
copy
How does a vector move?
vector
vector’s data
vector
nullptr nullptr nullptr
copy
How Did Move Semantics Get Started?
• Remember these fundamentals about move
semantics and vector, and you will have a
basic understanding of all of move semantics.
• The rest is just details…
Outline
• The genesis of move semantics
• Special member functions
• Introduction to the special move members
Special Members
• What are they?
Special Members
• Special members are those member
functions that the compiler can be asked
to automatically generate code for.
Special Members
• How many special members are there?
6
Special Members
• They are:
• default constructor
• destructor
• copy constructor
• copy assignment
• move constructor
• move assignment
X();
~X();
X(X const&);
X& operator=(X const&);
X(X&&);
X& operator=(X&&);
Special Members
• The special members can be:
• not declared
• implicitly declared
• user declared
defaulted
deleted
user-defined
or
or
or
Special Members
• What counts as user-declared?
struct X	
{	
X() {} // user-declared	
!
!
!
};
X(); // user-declared
X() = default; // user-declared
X() = delete; // user-declared
Special Members
• What is the difference between
not-declared and deleted?
Consider:
struct X	
{	
template <class ...Args>	
X(Args&& ...args);	
!
!
};
// The default constructor	
// is not declared
Special Members
• X can be default constructed by
using the variadic constructor.
struct X	
{	
template <class ...Args>	
X(Args&& ...args);	
!
!
};
// The default destructor	
// is not declared
Special Members
• Now X() binds to the deleted default constructor
instead of the variadic constructor.
• X is no longer default constructible.
struct X	
{	
template <class ...Args>	
X(Args&& ...args);	
!
!
};
X() = delete;
Special Members
• Deleted members participate in overload resolution.
• Members not-declared do not participate in
overload resolution.
struct X	
{	
template <class ...Args>	
X(Args&& ...args);	
!
!
};
X() = delete;
Special Members
• Under what circumstances are special
members implicitly provided?
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
userdeclares
compiler implicitly declares
Special Members
• If the user declares no special
members or constructors, all 6
special members will be defaulted.
• This part is no different from C++98/03
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
userdeclares
compiler implicitly declares
Special Members
• “defaulted” can mean “deleted” if
the defaulted special member would
have to do something illegal, such
as call another deleted function.
• Defaulted move members defined
as deleted, actually behave as not
declared.
• No, I’m not kidding!
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
userdeclares
compiler implicitly declares
Special Members
• If the user declares any non-special
constructor, this will inhibit the implicit
declaration of the default constructor.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
userdeclares
compiler implicitly declares
Special Members
• A user-declared default constructor will
not inhibit any other special member.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
userdeclares
compiler implicitly declares
Special Members
• A user-declared destructor will inhibit the implicit
declaration of the move members.
• The implicitly defaulted copy members are
deprecated.
• If you declare a destructor, declare your copy
members too, even though not necessary.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
copy
constructor
not
declared
defaulted
user
declared
defaulted
not
declared
not
declared
userdeclares
compiler implicitly declares
Special Members
• A user-declared copy constructor will
inhibit the default constructor and move
members.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
copy
constructor
not
declared
defaulted
user
declared
defaulted
not
declared
not
declared
copy
assignment
defaulted defaulted defaulted
user
declared
not
declared
not
declared
userdeclares
compiler implicitly declares
Special Members
• A user-declared copy assignment
will inhibit the move members.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
copy
constructor
not
declared
defaulted
user
declared
defaulted
not
declared
not
declared
copy
assignment
defaulted defaulted defaulted
user
declared
not
declared
not
declared
move
constructor
not
declared
defaulted deleted deleted
user
declared
not
declared
userdeclares
compiler implicitly declares
Special Members
• A user-declared move member will implicitly
delete the copy members.
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
copy
constructor
not
declared
defaulted
user
declared
defaulted
not
declared
not
declared
copy
assignment
defaulted defaulted defaulted
user
declared
not
declared
not
declared
move
constructor
not
declared
defaulted deleted deleted
user
declared
not
declared
move
assignment
defaulted defaulted deleted deleted
not
declared
user
declared
userdeclares
compiler implicitly declares
Special Members
default
constructor
destructor
copy
constructor
copy
assignment
Nothing defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
copy
constructor
not
declared
defaulted
user
declared
defaulted
copy
assignment
defaulted defaulted defaulted
user
declared
userdeclares
compiler implicitly declares
Special Members
This is
C++98/03
default
constructor
destructor
copy
constructor
copy
assignment
move
constructor
move
assignment
Nothing defaulted defaulted defaulted defaulted defaulted defaulted
Any
constructor
not
declared
defaulted defaulted defaulted defaulted defaulted
default
constructor
user
declared
defaulted defaulted defaulted defaulted defaulted
destructor defaulted
user
declared
defaulted defaulted
not
declared
not
declared
copy
constructor
not
declared
defaulted
user
declared
defaulted
not
declared
not
declared
copy
assignment
defaulted defaulted defaulted
user
declared
not
declared
not
declared
move
constructor
not
declared
defaulted deleted deleted
user
declared
not
declared
move
assignment
defaulted defaulted deleted deleted
not
declared
user
declared
userdeclares
compiler implicitly declares
Special Members
class X	
{	
public:	
!
!
!
!
!
!
};
X() = default;
~X() = default;
X(X const&) = default;
X& operator=(X const&) = default;
X& operator=(X&&) = default;
X(X&&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
X() = default;
~X() = default;
X(X const&) = default;
X& operator=(X const&) = default;
X& operator=(X&&) = default;
X(X&&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
X() = default;
~X() = default;
X(X const&) = default;
X& operator=(X const&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
~X() = default;
X(X const&) = default;
X& operator=(X const&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
X() = default;
~X() = default;
X(X const&) = default;
X& operator=(X const&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
~X() = default;
X(X const&) = delete;
X& operator=(X const&) = delete;
X(X&&) = default;
Special Members
An alternate presentation
of the same information.
class X	
{	
public:	
!
!
!
!
!
!
};
X() = default;
~X() = default;
X(X const&) = delete;
X& operator=(X const&) = delete;
X& operator=(X&&) = default;
Special Members
An alternate presentation
of the same information.
Outline
• The genesis of move semantics
• Special member functions
• Introduction to the special move members
What does a defaulted
move constructor do?
class X	
: public Base	
{	
Member m_;	
!
X(X&& x)	
: Base(static_cast<Base&&>(x))	
, m_(static_cast<Member&&>(x.m_))	
{}	
};
What does a typical user-
defined move constructor do?
class X	
: public Base	
{	
Member m_;	
!
X(X&& x)	
: Base(std::move(x))	
, m_(std::move(x.m_))	
{	
x.set_to_resourceless_state();	
}
What does a defaulted
move assignment do?
class X	
: public Base	
{	
Member m_;	
!
X& operator=(X&& x) {	
Base::operator=	
(static_cast<Base&&>(x));	
m_ = static_cast<Member&&>(x.m_);	
return *this;	
}
What does a typical user-
defined move assignment do?
class X	
: public Base	
{	
Member m_;	
!
X& operator=(X&& x) {	
Base::operator=(std::move(x));	
m_ = std::move(x.m_);	
x.set_to_resourceless_state();	
return *this;	
}
Can I define one special
member in terms of another?
Yes.
Should I define one special
member in terms of another?
No!
Should I define one special
member in terms of another?
No!
• Give each special member the tender
loving care it deserves.
• The entire point of move semantics is
to boost performance.
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
class X	
{	
std::vector<int> v_;	
public:	
X& operator=(X x) { // Implements	
v_.swap(x.v_); // both copy and	
return *this; // move assignment	
}	
}; What’s not to love?
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
class X	
{	
std::vector<int> v_;	
public:	
X& operator=(X const& x);	
X& operator=(X&& x);	
};
I’ve written highly
optimized versions of
the copy and move
assignment operators.
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
1
1.1
1.2
1.3
1.4
1.5
1.6
1.7
1.8
1.9
2
0% 25% 50% 75% 100%
How often is lhs capacity sufficient?
copy/swappenalty
Best case
(same speed)
Average case
(70% slower)
Worst case
(almost 8 times slower)
Speed of
optimized copy
assignment
operator vs “copy/
swap” assignment
lhs always needs to
reallocate vector
lhs never needs to
reallocate vector
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
How hard is it to make separate
optimized copy and move
assignment operators for this case?
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
class X	
{	
std::vector<int> v_;	
public:	
// Just keep your grubby fingers	
// off of the keyboard.	
// The defaults are optimal!	
!
}; What’s not to love?
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
But the copy/swap idiom gives
me strong exception safety!
Good point. Are all of your clients willing
to pay a giant performance penalty for
strong exception safety on assignment?
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
Perhaps you could interest the portion of
your clients that do need strong exception
safety in this generic function:
template <class C>	
C& strong_assign(C& dest, C src) {	
using std::swap;	
swap(dest, src);	
return dest;	
}
Should I define one special
member in terms of another?
Case study: the copy/swap idiom
Now clients who need speed can:
x = y;
And clients who need strong exception
safety can:
strong_assign(x, y);
In A Hurry?
• If you don’t have time to carefully consider all 6
special members, then just delete the copy
members:
class X	
{	
public:	
X(X const&) = delete;	
X& operator=(X const&) = delete;	
};
Summary
• Know when the compiler is defaulting or deleting
special members for you, and what defaulted
members will do.
• Always define or delete a special member when
the compiler’s implicit action is not correct.
• Give tender loving care to each of the 6 special
members, even if the result is to let the compiler
handle it.

Weitere ähnliche Inhalte

Was ist angesagt?

Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Olve Maudal
 
llvm basic porting for risc v
llvm basic porting for risc vllvm basic porting for risc v
llvm basic porting for risc vTsung-Chun Lin
 
Go Programming Patterns
Go Programming PatternsGo Programming Patterns
Go Programming PatternsHao Chen
 
NGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEANGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEANGINX, Inc.
 
Open source apm scouter를 통한 관제 관리 jadecross 정환열 수석
Open source apm scouter를 통한 관제  관리 jadecross 정환열 수석Open source apm scouter를 통한 관제  관리 jadecross 정환열 수석
Open source apm scouter를 통한 관제 관리 jadecross 정환열 수석uEngine Solutions
 
Introduction to the LLVM Compiler System
Introduction to the LLVM  Compiler SystemIntroduction to the LLVM  Compiler System
Introduction to the LLVM Compiler Systemzionsaint
 
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트) 마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트) Amazon Web Services Korea
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic LinkingWang Hsiangkai
 
MSA ( Microservices Architecture ) 발표 자료 다운로드
MSA ( Microservices Architecture ) 발표 자료 다운로드MSA ( Microservices Architecture ) 발표 자료 다운로드
MSA ( Microservices Architecture ) 발표 자료 다운로드Opennaru, inc.
 
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...The Linux Foundation
 
[KGC 2011]Boost 라이브러리와 C++11
[KGC 2011]Boost 라이브러리와 C++11[KGC 2011]Boost 라이브러리와 C++11
[KGC 2011]Boost 라이브러리와 C++11흥배 최
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020Ji-Woong Choi
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsDinesh U
 

Was ist angesagt? (20)

Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)Modern C++ Explained: Move Semantics (Feb 2018)
Modern C++ Explained: Move Semantics (Feb 2018)
 
GCC LTO
GCC LTOGCC LTO
GCC LTO
 
Nginx
NginxNginx
Nginx
 
llvm basic porting for risc v
llvm basic porting for risc vllvm basic porting for risc v
llvm basic porting for risc v
 
What Can Compilers Do for Us?
What Can Compilers Do for Us?What Can Compilers Do for Us?
What Can Compilers Do for Us?
 
LLVM
LLVMLLVM
LLVM
 
Go Programming Patterns
Go Programming PatternsGo Programming Patterns
Go Programming Patterns
 
Xvisor: embedded and lightweight hypervisor
Xvisor: embedded and lightweight hypervisorXvisor: embedded and lightweight hypervisor
Xvisor: embedded and lightweight hypervisor
 
NGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEANGINX: Basics and Best Practices EMEA
NGINX: Basics and Best Practices EMEA
 
Open source apm scouter를 통한 관제 관리 jadecross 정환열 수석
Open source apm scouter를 통한 관제  관리 jadecross 정환열 수석Open source apm scouter를 통한 관제  관리 jadecross 정환열 수석
Open source apm scouter를 통한 관제 관리 jadecross 정환열 수석
 
Introduction to the LLVM Compiler System
Introduction to the LLVM  Compiler SystemIntroduction to the LLVM  Compiler System
Introduction to the LLVM Compiler System
 
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트) 마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
마이크로서비스 기반 클라우드 아키텍처 구성 모범 사례 - 윤석찬 (AWS 테크에반젤리스트)
 
Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
MSA ( Microservices Architecture ) 발표 자료 다운로드
MSA ( Microservices Architecture ) 발표 자료 다운로드MSA ( Microservices Architecture ) 발표 자료 다운로드
MSA ( Microservices Architecture ) 발표 자료 다운로드
 
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
Rootlinux17: Hypervisors on ARM - Overview and Design Choices by Julien Grall...
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
[KGC 2011]Boost 라이브러리와 C++11
[KGC 2011]Boost 라이브러리와 C++11[KGC 2011]Boost 라이브러리와 C++11
[KGC 2011]Boost 라이브러리와 C++11
 
Memory model
Memory modelMemory model
Memory model
 
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020[오픈소스컨설팅] 스카우터 사용자 가이드 2020
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 

Andere mochten auch

Ripple Labs: Values, Purpose, Strategy 2014
Ripple Labs: Values, Purpose, Strategy 2014Ripple Labs: Values, Purpose, Strategy 2014
Ripple Labs: Values, Purpose, Strategy 2014Ripple Labs
 
Types Don't Know #, Howard Hinnant - CppCon 2014
Types Don't Know #, Howard Hinnant - CppCon 2014Types Don't Know #, Howard Hinnant - CppCon 2014
Types Don't Know #, Howard Hinnant - CppCon 2014Ripple Labs
 
Ripple Developer Conference 2013 at Money2020
Ripple Developer Conference 2013 at Money2020Ripple Developer Conference 2013 at Money2020
Ripple Developer Conference 2013 at Money2020Ripple Labs
 
Blockchain powering the internet of value
Blockchain powering the internet of value  Blockchain powering the internet of value
Blockchain powering the internet of value Diego Alberto Tamayo
 
Ripple勉強会20150306
Ripple勉強会20150306Ripple勉強会20150306
Ripple勉強会20150306Yuki Akiyama
 
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoins
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoinsContracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoins
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoinsRipple Labs
 
An Introduction to Ripple XRP
An Introduction to Ripple XRPAn Introduction to Ripple XRP
An Introduction to Ripple XRPTradefast
 
20141107 rippleとはなにか?仕組みと可能性
20141107 rippleとはなにか?仕組みと可能性20141107 rippleとはなにか?仕組みと可能性
20141107 rippleとはなにか?仕組みと可能性Tetsuyuki Oishi
 
리플코인자료
리플코인자료리플코인자료
리플코인자료Johnny Lee
 
리플코인 (1) 1
리플코인 (1) 1리플코인 (1) 1
리플코인 (1) 1Johnny Lee
 
Chinese UI design guidelines 2.0
Chinese UI design guidelines 2.0Chinese UI design guidelines 2.0
Chinese UI design guidelines 2.0Jan Brejcha
 
Financial literacy in China as an innovation opportunity
Financial literacy in China as an innovation opportunityFinancial literacy in China as an innovation opportunity
Financial literacy in China as an innovation opportunityJan Brejcha
 
Ripple future of payments is here
Ripple   future of payments is hereRipple   future of payments is here
Ripple future of payments is hereWojciech Ziniewicz
 
Ripple - the good, the bad and the ugly
Ripple - the good, the bad and the uglyRipple - the good, the bad and the ugly
Ripple - the good, the bad and the uglyBlockchainHub Graz
 
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...Sherry Jones
 
Масштабируемость блокчейн-систем: проблемы и решения
Масштабируемость блокчейн-систем: проблемы и решенияМасштабируемость блокчейн-систем: проблемы и решения
Масштабируемость блокчейн-систем: проблемы и решенияAlex Chepurnoy
 
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoin
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoinRobert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoin
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoinRobert Jan Vrolijk
 
Blockchain Angels & UK Business Angels Association Oct 16
Blockchain Angels & UK Business Angels Association Oct 16Blockchain Angels & UK Business Angels Association Oct 16
Blockchain Angels & UK Business Angels Association Oct 16Jamie Burke
 

Andere mochten auch (20)

Ripple Labs: Values, Purpose, Strategy 2014
Ripple Labs: Values, Purpose, Strategy 2014Ripple Labs: Values, Purpose, Strategy 2014
Ripple Labs: Values, Purpose, Strategy 2014
 
Types Don't Know #, Howard Hinnant - CppCon 2014
Types Don't Know #, Howard Hinnant - CppCon 2014Types Don't Know #, Howard Hinnant - CppCon 2014
Types Don't Know #, Howard Hinnant - CppCon 2014
 
Ripple Developer Conference 2013 at Money2020
Ripple Developer Conference 2013 at Money2020Ripple Developer Conference 2013 at Money2020
Ripple Developer Conference 2013 at Money2020
 
Blockchain powering the internet of value
Blockchain powering the internet of value  Blockchain powering the internet of value
Blockchain powering the internet of value
 
Ripple勉強会20150306
Ripple勉強会20150306Ripple勉強会20150306
Ripple勉強会20150306
 
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoins
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoinsContracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoins
Contracts Across Coins - Smart Contracts for Bitcoin, Ripple and the altcoins
 
An Introduction to Ripple XRP
An Introduction to Ripple XRPAn Introduction to Ripple XRP
An Introduction to Ripple XRP
 
20141107 rippleとはなにか?仕組みと可能性
20141107 rippleとはなにか?仕組みと可能性20141107 rippleとはなにか?仕組みと可能性
20141107 rippleとはなにか?仕組みと可能性
 
리플코인자료
리플코인자료리플코인자료
리플코인자료
 
리플코인 (1) 1
리플코인 (1) 1리플코인 (1) 1
리플코인 (1) 1
 
Chinese UI design guidelines 2.0
Chinese UI design guidelines 2.0Chinese UI design guidelines 2.0
Chinese UI design guidelines 2.0
 
StromDAO - Die Idee
StromDAO - Die IdeeStromDAO - Die Idee
StromDAO - Die Idee
 
Financial literacy in China as an innovation opportunity
Financial literacy in China as an innovation opportunityFinancial literacy in China as an innovation opportunity
Financial literacy in China as an innovation opportunity
 
Ripple future of payments is here
Ripple   future of payments is hereRipple   future of payments is here
Ripple future of payments is here
 
Ripple - the good, the bad and the ugly
Ripple - the good, the bad and the uglyRipple - the good, the bad and the ugly
Ripple - the good, the bad and the ugly
 
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...
Understanding Stories of Diversity, Inclusion, and Tolerance Through Games (F...
 
Масштабируемость блокчейн-систем: проблемы и решения
Масштабируемость блокчейн-систем: проблемы и решенияМасштабируемость блокчейн-систем: проблемы и решения
Масштабируемость блокчейн-систем: проблемы и решения
 
A Strategist's Guide to Blockchain
A Strategist's Guide to BlockchainA Strategist's Guide to Blockchain
A Strategist's Guide to Blockchain
 
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoin
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoinRobert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoin
Robert jan-vrolijk--why-do-banks-prefer-ripple-over-bitcoin
 
Blockchain Angels & UK Business Angels Association Oct 16
Blockchain Angels & UK Business Angels Association Oct 16Blockchain Angels & UK Business Angels Association Oct 16
Blockchain Angels & UK Business Angels Association Oct 16
 

Ähnlich wie Everything You Ever Wanted to Know About Move Semantics, Howard Hinnant, Accu 2014

Modelling Microservices at Spotify - Petter Mahlen
Modelling Microservices at Spotify - Petter MahlenModelling Microservices at Spotify - Petter Mahlen
Modelling Microservices at Spotify - Petter MahlenJ On The Beach
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmodwalkmod
 
Bridging the Divide between Architecture and Code (Germany JUGs version)
Bridging the Divide between Architecture and Code (Germany JUGs version)Bridging the Divide between Architecture and Code (Germany JUGs version)
Bridging the Divide between Architecture and Code (Germany JUGs version)Chris Chedgey
 
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other toolskinan keshkeh
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Fwdays
 
scaling compiled applications - highload 2013
scaling compiled applications - highload 2013scaling compiled applications - highload 2013
scaling compiled applications - highload 2013ice799
 
Joe Damato
Joe DamatoJoe Damato
Joe DamatoOntico
 
Abhilash front end ppt.pdf
Abhilash front end ppt.pdfAbhilash front end ppt.pdf
Abhilash front end ppt.pdfvinodpatil406281
 
Project Tools in Web Development
Project Tools in Web DevelopmentProject Tools in Web Development
Project Tools in Web Developmentkmloomis
 
saikiran front end ppt.pptx
saikiran front end ppt.pptxsaikiran front end ppt.pptx
saikiran front end ppt.pptxvinodpatil406281
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareChris Weldon
 
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...Kostas Mavridis
 
Features, Exportables & You
Features, Exportables & YouFeatures, Exportables & You
Features, Exportables & Youjskulski
 
How to analyze your codebase with Exakat using Docker - Longhorn PHP
How to analyze your codebase with Exakat using Docker - Longhorn PHPHow to analyze your codebase with Exakat using Docker - Longhorn PHP
How to analyze your codebase with Exakat using Docker - Longhorn PHPDana Luther
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.jsIvano Malavolta
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxsandeshshahapur
 
Infrastructure as Code Patterns
Infrastructure as Code PatternsInfrastructure as Code Patterns
Infrastructure as Code PatternsKris Buytaert
 
stackconf 2022: Infrastructure Automation (anti) patterns
stackconf 2022: Infrastructure Automation (anti) patternsstackconf 2022: Infrastructure Automation (anti) patterns
stackconf 2022: Infrastructure Automation (anti) patternsNETWAYS
 

Ähnlich wie Everything You Ever Wanted to Know About Move Semantics, Howard Hinnant, Accu 2014 (20)

Modelling Microservices at Spotify - Petter Mahlen
Modelling Microservices at Spotify - Petter MahlenModelling Microservices at Spotify - Petter Mahlen
Modelling Microservices at Spotify - Petter Mahlen
 
33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod33rd degree talk: open and automatic coding conventions with walkmod
33rd degree talk: open and automatic coding conventions with walkmod
 
Bridging the Divide between Architecture and Code (Germany JUGs version)
Bridging the Divide between Architecture and Code (Germany JUGs version)Bridging the Divide between Architecture and Code (Germany JUGs version)
Bridging the Divide between Architecture and Code (Germany JUGs version)
 
2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools2 BytesC++ course_2014_c6_ constructors and other tools
2 BytesC++ course_2014_c6_ constructors and other tools
 
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
Алексей Ященко и Ярослав Волощук "False simplicity of front-end applications"
 
scaling compiled applications - highload 2013
scaling compiled applications - highload 2013scaling compiled applications - highload 2013
scaling compiled applications - highload 2013
 
Joe Damato
Joe DamatoJoe Damato
Joe Damato
 
Abhilash front end ppt.pdf
Abhilash front end ppt.pdfAbhilash front end ppt.pdf
Abhilash front end ppt.pdf
 
Project Tools in Web Development
Project Tools in Web DevelopmentProject Tools in Web Development
Project Tools in Web Development
 
saikiran front end ppt.pptx
saikiran front end ppt.pptxsaikiran front end ppt.pptx
saikiran front end ppt.pptx
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver SoftwareBeyond TDD: Enabling Your Team to Continuously Deliver Software
Beyond TDD: Enabling Your Team to Continuously Deliver Software
 
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...
EoinWoods_WhereDidMyArchitectureGoPreservingSoftwareArchitectureInItsImplemen...
 
Features, Exportables & You
Features, Exportables & YouFeatures, Exportables & You
Features, Exportables & You
 
How to analyze your codebase with Exakat using Docker - Longhorn PHP
How to analyze your codebase with Exakat using Docker - Longhorn PHPHow to analyze your codebase with Exakat using Docker - Longhorn PHP
How to analyze your codebase with Exakat using Docker - Longhorn PHP
 
Handlebars & Require JS
Handlebars  & Require JSHandlebars  & Require JS
Handlebars & Require JS
 
Handlebars and Require.js
Handlebars and Require.jsHandlebars and Require.js
Handlebars and Require.js
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Infrastructure as Code Patterns
Infrastructure as Code PatternsInfrastructure as Code Patterns
Infrastructure as Code Patterns
 
stackconf 2022: Infrastructure Automation (anti) patterns
stackconf 2022: Infrastructure Automation (anti) patternsstackconf 2022: Infrastructure Automation (anti) patterns
stackconf 2022: Infrastructure Automation (anti) patterns
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
 

Kürzlich hochgeladen

Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleAlluxio, Inc.
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 

Kürzlich hochgeladen (20)

Correctly Loading Incremental Data at Scale
Correctly Loading Incremental Data at ScaleCorrectly Loading Incremental Data at Scale
Correctly Loading Incremental Data at Scale
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 

Everything You Ever Wanted to Know About Move Semantics, Howard Hinnant, Accu 2014

  • 1. Everything You Ever Wanted To Know About Move Semantics Howard Hinnant Senior Software Engineer Ripple Labs ! April 12, 2014 (and then some)
  • 2. • The genesis of move semantics • Special member functions • Introduction to the special move members Outline
  • 3. How Did Move Semantics Get Started? • It was all about optimizing std::vector<T>. • And everything else just rode along on its coattails.
  • 4. vector What is std::vector? • Anatomy of a vector (simplified) vector’s data begin() end() capacity()
  • 5. How does a vector copy? vector vector’s data vector vector’s data copy
  • 6. How does a vector move? vector vector’s data vector nullptr nullptr nullptr copy
  • 7. How Did Move Semantics Get Started? • Remember these fundamentals about move semantics and vector, and you will have a basic understanding of all of move semantics. • The rest is just details…
  • 8. Outline • The genesis of move semantics • Special member functions • Introduction to the special move members
  • 10. Special Members • Special members are those member functions that the compiler can be asked to automatically generate code for.
  • 11. Special Members • How many special members are there? 6
  • 12. Special Members • They are: • default constructor • destructor • copy constructor • copy assignment • move constructor • move assignment X(); ~X(); X(X const&); X& operator=(X const&); X(X&&); X& operator=(X&&);
  • 13. Special Members • The special members can be: • not declared • implicitly declared • user declared defaulted deleted user-defined or or or
  • 14. Special Members • What counts as user-declared? struct X { X() {} // user-declared ! ! ! }; X(); // user-declared X() = default; // user-declared X() = delete; // user-declared
  • 15. Special Members • What is the difference between not-declared and deleted? Consider: struct X { template <class ...Args> X(Args&& ...args); ! ! }; // The default constructor // is not declared
  • 16. Special Members • X can be default constructed by using the variadic constructor. struct X { template <class ...Args> X(Args&& ...args); ! ! }; // The default destructor // is not declared
  • 17. Special Members • Now X() binds to the deleted default constructor instead of the variadic constructor. • X is no longer default constructible. struct X { template <class ...Args> X(Args&& ...args); ! ! }; X() = delete;
  • 18. Special Members • Deleted members participate in overload resolution. • Members not-declared do not participate in overload resolution. struct X { template <class ...Args> X(Args&& ...args); ! ! }; X() = delete;
  • 19. Special Members • Under what circumstances are special members implicitly provided?
  • 20. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • If the user declares no special members or constructors, all 6 special members will be defaulted. • This part is no different from C++98/03
  • 21. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • “defaulted” can mean “deleted” if the defaulted special member would have to do something illegal, such as call another deleted function. • Defaulted move members defined as deleted, actually behave as not declared. • No, I’m not kidding!
  • 22. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • If the user declares any non-special constructor, this will inhibit the implicit declaration of the default constructor.
  • 23. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted userdeclares compiler implicitly declares Special Members • A user-declared default constructor will not inhibit any other special member.
  • 24. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared destructor will inhibit the implicit declaration of the move members. • The implicitly defaulted copy members are deprecated. • If you declare a destructor, declare your copy members too, even though not necessary.
  • 25. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared copy constructor will inhibit the default constructor and move members.
  • 26. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared userdeclares compiler implicitly declares Special Members • A user-declared copy assignment will inhibit the move members.
  • 27. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared userdeclares compiler implicitly declares Special Members • A user-declared move member will implicitly delete the copy members.
  • 28. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared move assignment defaulted defaulted deleted deleted not declared user declared userdeclares compiler implicitly declares Special Members
  • 29. default constructor destructor copy constructor copy assignment Nothing defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted copy constructor not declared defaulted user declared defaulted copy assignment defaulted defaulted defaulted user declared userdeclares compiler implicitly declares Special Members This is C++98/03
  • 30. default constructor destructor copy constructor copy assignment move constructor move assignment Nothing defaulted defaulted defaulted defaulted defaulted defaulted Any constructor not declared defaulted defaulted defaulted defaulted defaulted default constructor user declared defaulted defaulted defaulted defaulted defaulted destructor defaulted user declared defaulted defaulted not declared not declared copy constructor not declared defaulted user declared defaulted not declared not declared copy assignment defaulted defaulted defaulted user declared not declared not declared move constructor not declared defaulted deleted deleted user declared not declared move assignment defaulted defaulted deleted deleted not declared user declared userdeclares compiler implicitly declares Special Members
  • 31. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; X& operator=(X&&) = default; X(X&&) = default; Special Members An alternate presentation of the same information.
  • 32. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; X& operator=(X&&) = default; X(X&&) = default; Special Members An alternate presentation of the same information.
  • 33. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  • 34. class X { public: ! ! ! ! ! ! }; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  • 35. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = default; X& operator=(X const&) = default; Special Members An alternate presentation of the same information.
  • 36. class X { public: ! ! ! ! ! ! }; ~X() = default; X(X const&) = delete; X& operator=(X const&) = delete; X(X&&) = default; Special Members An alternate presentation of the same information.
  • 37. class X { public: ! ! ! ! ! ! }; X() = default; ~X() = default; X(X const&) = delete; X& operator=(X const&) = delete; X& operator=(X&&) = default; Special Members An alternate presentation of the same information.
  • 38. Outline • The genesis of move semantics • Special member functions • Introduction to the special move members
  • 39. What does a defaulted move constructor do? class X : public Base { Member m_; ! X(X&& x) : Base(static_cast<Base&&>(x)) , m_(static_cast<Member&&>(x.m_)) {} };
  • 40. What does a typical user- defined move constructor do? class X : public Base { Member m_; ! X(X&& x) : Base(std::move(x)) , m_(std::move(x.m_)) { x.set_to_resourceless_state(); }
  • 41. What does a defaulted move assignment do? class X : public Base { Member m_; ! X& operator=(X&& x) { Base::operator= (static_cast<Base&&>(x)); m_ = static_cast<Member&&>(x.m_); return *this; }
  • 42. What does a typical user- defined move assignment do? class X : public Base { Member m_; ! X& operator=(X&& x) { Base::operator=(std::move(x)); m_ = std::move(x.m_); x.set_to_resourceless_state(); return *this; }
  • 43. Can I define one special member in terms of another? Yes.
  • 44. Should I define one special member in terms of another? No!
  • 45. Should I define one special member in terms of another? No! • Give each special member the tender loving care it deserves. • The entire point of move semantics is to boost performance.
  • 46. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: X& operator=(X x) { // Implements v_.swap(x.v_); // both copy and return *this; // move assignment } }; What’s not to love?
  • 47. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: X& operator=(X const& x); X& operator=(X&& x); }; I’ve written highly optimized versions of the copy and move assignment operators.
  • 48. Should I define one special member in terms of another? Case study: the copy/swap idiom 1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2 0% 25% 50% 75% 100% How often is lhs capacity sufficient? copy/swappenalty Best case (same speed) Average case (70% slower) Worst case (almost 8 times slower) Speed of optimized copy assignment operator vs “copy/ swap” assignment lhs always needs to reallocate vector lhs never needs to reallocate vector
  • 49. Should I define one special member in terms of another? Case study: the copy/swap idiom How hard is it to make separate optimized copy and move assignment operators for this case?
  • 50. Should I define one special member in terms of another? Case study: the copy/swap idiom class X { std::vector<int> v_; public: // Just keep your grubby fingers // off of the keyboard. // The defaults are optimal! ! }; What’s not to love?
  • 51. Should I define one special member in terms of another? Case study: the copy/swap idiom But the copy/swap idiom gives me strong exception safety! Good point. Are all of your clients willing to pay a giant performance penalty for strong exception safety on assignment?
  • 52. Should I define one special member in terms of another? Case study: the copy/swap idiom Perhaps you could interest the portion of your clients that do need strong exception safety in this generic function: template <class C> C& strong_assign(C& dest, C src) { using std::swap; swap(dest, src); return dest; }
  • 53. Should I define one special member in terms of another? Case study: the copy/swap idiom Now clients who need speed can: x = y; And clients who need strong exception safety can: strong_assign(x, y);
  • 54. In A Hurry? • If you don’t have time to carefully consider all 6 special members, then just delete the copy members: class X { public: X(X const&) = delete; X& operator=(X const&) = delete; };
  • 55. Summary • Know when the compiler is defaulting or deleting special members for you, and what defaulted members will do. • Always define or delete a special member when the compiler’s implicit action is not correct. • Give tender loving care to each of the 6 special members, even if the result is to let the compiler handle it.