SlideShare a Scribd company logo
わんくま同盟 横浜勉強会 #06
C++11 : 右辺値参照 ておいしいの?
わんくま同盟
episthmh epi@c3-net.ne.jp
わんくま同盟 横浜勉強会 #06
C++ は むずかしいややこしい
class Foo;
void f(Foo x); ← 値 (C)
void f(Foo* x); ← ポインタ (C)
void f(Foo& x); ← 参照 (C++)
...こんだけでも十分ややこしいのに
void f(Foo&& x); ← 右辺値参照(C++11 NEW)
わんくま同盟 横浜勉強会 #06
そもそも「右辺値(R-value)」てナニよ?
z = f( x+1 ) + 2 * y;
すっごくテキトーに言えば
「その場限りに作られた値」
→ 代入や関数に渡されたら用済みとなる
= 廃棄される MOTTAINAI!
わんくま同盟 横浜勉強会 #06
move semantics
y = x; は、
フツーなら “x を y に copy” だけど
x が右辺値なら
右辺を copy の後 廃棄するんだから
意味的には move だよね。
copy+廃棄 コスト > move コスト なら
move の方がお得よね♪
わんくま同盟 横浜勉強会 #06
copy より move がお得な典型例
template<typename T>
class novice_vector {
private:
T* data_; // Tの列
size_t size_; // 要素数
size_t capacity_; // 容量
…
};
あいうえお
よくある
コンテナ/コレクション
わんくま同盟 横浜勉強会 #06
copy ctor
novice_vector(const novice_vector& other)
: size_(other.size_), capacity_(other.capacity_) {
// 領域を確保して
data_ = new T[capacity_];
// 引数からcopyする
for ( size_t i = 0; i < size_; ++i ) {
data_[i] = other.data_[i];
}
}
わんくま同盟 横浜勉強会 #06
copy operator ‘=‘
novice_vector& operator=(const novice_vector& other) {
// 現data_を廃棄し
delete[] data_;
// 新たに領域を確保して
data_ = new T[other.cpacity_];
// 右辺からcopyする
for ( size_t i = 0; i < other.size_; ++i ) {
data_[i] = other.data_[i];
}
size_ = other.size_;
capacity_ = other_.capacity_;
return *this;
}
わんくま同盟 横浜勉強会 #06
novice_vector<T> のcopy
あいうえお ABCD
あいうえお
????
左辺 右辺
ABCD
ABCD
廃棄
生成
わんくま同盟 横浜勉強会 #06
novice_vector<T> のcopy
ABCD ABCD
ABCD
左辺 右辺
ABCD
2 廃棄
1 生成
1 コピー
廃棄
コピー
わんくま同盟 横浜勉強会 #06
move ctor
novice_vector(novice_vector&& other)
// 右辺のナカミをもらって
: size_(other.size_), capacity_(other.capacity_) {
data_ = other.data_;
// 右辺側は空にする
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
}
わんくま同盟 横浜勉強会 #06
move operator ‘=‘
novice_vector& operator=(novice_vector&& other) {
// 右辺のナカミをもらって
size_ = other.size_;
capacity_ = other.capacity_;
data_ = other.data_;
// 右辺側は空にする
other.data_ = nullptr;
other.size_ = 0;
other.capacity_ = 0;
return *this;
}
わんくま同盟 横浜勉強会 #06
novice_vector<T> のmove
あいうえお ABCD
あいうえお
左辺 右辺値
ABCD
ABCD
nullptr
1 廃棄
0 生成
0 コピー
廃棄

More Related Content

What's hot

Implicit Implicit Scala
Implicit Implicit ScalaImplicit Implicit Scala
Implicit Implicit Scala
Kota Mizushima
 
こわくない型クラス
こわくない型クラスこわくない型クラス
こわくない型クラス
Kota Mizushima
 

What's hot (20)

C言語講習会1
C言語講習会1C言語講習会1
C言語講習会1
 
ナウなヤングにバカうけのイカしたタグ付き共用体
ナウなヤングにバカうけのイカしたタグ付き共用体ナウなヤングにバカうけのイカしたタグ付き共用体
ナウなヤングにバカうけのイカしたタグ付き共用体
 
Scala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみたScala 初心者が Hom 函手を Scala で考えてみた
Scala 初心者が Hom 函手を Scala で考えてみた
 
Scala 初心者が米田の補題を Scala で考えてみた
Scala 初心者が米田の補題を Scala で考えてみたScala 初心者が米田の補題を Scala で考えてみた
Scala 初心者が米田の補題を Scala で考えてみた
 
Implicit Explicit Scala
Implicit Explicit ScalaImplicit Explicit Scala
Implicit Explicit Scala
 
Implicit Implicit Scala
Implicit Implicit ScalaImplicit Implicit Scala
Implicit Implicit Scala
 
kagamicomput201807
kagamicomput201807kagamicomput201807
kagamicomput201807
 
Web05
Web05Web05
Web05
 
C言語講習会2
C言語講習会2C言語講習会2
C言語講習会2
 
Introduction to lambda calculation
Introduction to lambda calculationIntroduction to lambda calculation
Introduction to lambda calculation
 
Introduction to Categorical Programming
Introduction to Categorical ProgrammingIntroduction to Categorical Programming
Introduction to Categorical Programming
 
5 Info Theory
5 Info Theory5 Info Theory
5 Info Theory
 
Startprintf_2013May18
Startprintf_2013May18Startprintf_2013May18
Startprintf_2013May18
 
こわくない型クラス
こわくない型クラスこわくない型クラス
こわくない型クラス
 
やさしく知りたいC言語
やさしく知りたいC言語やさしく知りたいC言語
やさしく知りたいC言語
 
#6:実数と繰り返し
#6:実数と繰り返し#6:実数と繰り返し
#6:実数と繰り返し
 
SICP
SICPSICP
SICP
 
Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)
 
Ilerpg Study 002
Ilerpg Study 002Ilerpg Study 002
Ilerpg Study 002
 
C++ tips 3 カンマ演算子編
C++ tips 3 カンマ演算子編C++ tips 3 カンマ演算子編
C++ tips 3 カンマ演算子編
 

Viewers also liked (9)

T77 episteme
T77 epistemeT77 episteme
T77 episteme
 
ぱっと見でわかるC++11
ぱっと見でわかるC++11ぱっと見でわかるC++11
ぱっと見でわかるC++11
 
T69 episteme
T69 epistemeT69 episteme
T69 episteme
 
T45 episteme
T45 epistemeT45 episteme
T45 episteme
 
Episteme unique_ptr
Episteme unique_ptrEpisteme unique_ptr
Episteme unique_ptr
 
Episteme variadic template
Episteme variadic templateEpisteme variadic template
Episteme variadic template
 
.NETラボ 2013-12-21 LT
.NETラボ 2013-12-21 LT.NETラボ 2013-12-21 LT
.NETラボ 2013-12-21 LT
 
中3女子でもわかる constexpr
中3女子でもわかる constexpr中3女子でもわかる constexpr
中3女子でもわかる constexpr
 
BoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうかBoostAsioで可読性を求めるのは間違っているだろうか
BoostAsioで可読性を求めるのは間違っているだろうか
 

Recently uploaded

2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
ssuserbefd24
 

Recently uploaded (10)

YugabyteDB適用に向けた取り組みと隠れた魅力 (DSS Asia 2024 発表資料)
YugabyteDB適用に向けた取り組みと隠れた魅力 (DSS Asia 2024 発表資料)YugabyteDB適用に向けた取り組みと隠れた魅力 (DSS Asia 2024 発表資料)
YugabyteDB適用に向けた取り組みと隠れた魅力 (DSS Asia 2024 発表資料)
 
【AI論文解説】Consistency ModelとRectified Flow
【AI論文解説】Consistency ModelとRectified Flow【AI論文解説】Consistency ModelとRectified Flow
【AI論文解説】Consistency ModelとRectified Flow
 
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアルLoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
LoRaWAN 4チャンネル電流センサー・コンバーター CS01-LB 日本語マニュアル
 
20240523_IoTLT_vol111_kitazaki_v1___.pdf
20240523_IoTLT_vol111_kitazaki_v1___.pdf20240523_IoTLT_vol111_kitazaki_v1___.pdf
20240523_IoTLT_vol111_kitazaki_v1___.pdf
 
論文紹介: Offline Q-Learning on diverse Multi-Task data both scales and generalizes
論文紹介: Offline Q-Learning on diverse Multi-Task data both scales and generalizes論文紹介: Offline Q-Learning on diverse Multi-Task data both scales and generalizes
論文紹介: Offline Q-Learning on diverse Multi-Task data both scales and generalizes
 
MPAなWebフレームワーク、Astroの紹介 (その2) 2024/05/24の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その2) 2024/05/24の勉強会で発表されたものです。MPAなWebフレームワーク、Astroの紹介 (その2) 2024/05/24の勉強会で発表されたものです。
MPAなWebフレームワーク、Astroの紹介 (その2) 2024/05/24の勉強会で発表されたものです。
 
2024年度_サイバーエージェント_新卒研修「データベースの歴史」.pptx
2024年度_サイバーエージェント_新卒研修「データベースの歴史」.pptx2024年度_サイバーエージェント_新卒研修「データベースの歴史」.pptx
2024年度_サイバーエージェント_新卒研修「データベースの歴史」.pptx
 
論文紹介: Exploiting semantic segmentation to boost reinforcement learning in vid...
論文紹介: Exploiting semantic segmentation to boost reinforcement learning in vid...論文紹介: Exploiting semantic segmentation to boost reinforcement learning in vid...
論文紹介: Exploiting semantic segmentation to boost reinforcement learning in vid...
 
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
Amazon Cognitoで実装するパスキー (Security-JAWS【第33回】 勉強会)
 
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
2024年5月25日Serverless Meetup大阪 アプリケーションをどこで動かすべきなのか.pptx
 

Yokohama6 epi