Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Effective Modern C++ 読書会 Item29, Item30
Search
Linda_pp
July 16, 2015
Technology
0
450
Effective Modern C++ 読書会 Item29, Item30
Item29: move に期待しすぎないようにしよう
Item30: perfect forwarding に失敗する場合を知ろう
Linda_pp
July 16, 2015
Tweet
Share
More Decks by Linda_pp
See All by Linda_pp
actionlint の Linter 設計
rhysd
6
4.8k
ripgrep をライブラリとして使う
rhysd
0
440
port-monolith-to-wasm-for-chrome-extension
rhysd
0
470
Fuzzing Rust Text Editor
rhysd
1
3k
Vim compiled to WebAssembly
rhysd
5
2.2k
about-neovim-0.4.0-floating-window
rhysd
3
2.2k
reply.vim
rhysd
0
1.2k
Vim ported to WebAssembly (VimConf 2018)
rhysd
4
3.3k
go-selfupdate-github で ツールを自己アップデートする
rhysd
5
4.3k
Other Decks in Technology
See All in Technology
Share my, our lessons from the road to re:Invent
naospon
0
130
LINE NEWSにおけるバックエンド開発
lycorptech_jp
PRO
0
120
システム・ML活用を広げるdbtのデータモデリング / Expanding System & ML Use with dbt Modeling
i125
1
310
一度 Expo の採用を断念したけど、 再度 Expo の導入を検討している話
ichiki1023
1
250
php-conference-nagoya-2025
fuwasegu
0
140
Snowflakeの開発・運用コストをApache Icebergで効率化しよう!~機能と活用例のご紹介~
sagara
1
270
Raycast AI APIを使ってちょっと便利な拡張機能を作ってみた / created-a-handy-extension-using-the-raycast-ai-api
kawamataryo
0
180
「正しく」失敗できる チームの作り方 〜リアルな事例から紐解く失敗を恐れない組織とは〜 / A team that can fail correctly
i35_267
2
700
利用終了したドメイン名の最強終活〜観測環境を育てて、分析・供養している件〜 / The Ultimate End-of-Life Preparation for Discontinued Domain Names
nttcom
2
360
表現を育てる
kiyou77
1
230
次世代KYC活動報告 / 20250219-BizDay17-KYC-nextgen
oidfj
0
460
MIMEと文字コードの闇
hirachan
2
1.4k
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.2k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
21
2.5k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Site-Speed That Sticks
csswizardry
4
400
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Why Our Code Smells
bkeepers
PRO
336
57k
Building a Scalable Design System with Sketch
lauravandoore
461
33k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
100
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
33
2.8k
Transcript
Effective Modern C++ ಡॻձ Item29, Item 30 @Linda_pp @rhysd
Item 29: moveʹظ͗͢͠Δͳ ʢAssume that move operations are not present,
not cheap, and not used)
move • move C++11 ͷۄػೳͷͻͱͭ • C++03 ͷίʔυ C++11
ͰϏϧυ͠ͳ͓ͤ ͘ͳΔʂ • STL move ରԠ • ίϯςφͷίϐʔ͕ϙΠϯλ1ͭͷίϐʔʹͳΔ ͧʂ
࣮ࡍ move ͕ࢥͬͨ΄Ͳ༗ ༻Ͱͳ͍߹͕ͨ͘͞Μ͋Δ
move ૢ࡞͕ଘࡏ͠ͳ͍࣌ ҉ͷ move Λఆٛ͞ΕΔ͕݅ݫ͍͠ • copyૢ࡞ɼmoveૢ࡞ɼσετϥΫλ͕ Ϣʔβఆٛ͞Ε͍ͯͳ͍ (Item17)
• جఈΫϥε͕moveඇରԠͰͳ͍ (Item11)
move ૢ࡞ͷίετ͕͘ͳ ͍࣌ ΄ͱΜͲͷ STL ίϯςφͷ move → ώʔϓ ͷϙΠϯλ
ͷషΓସ͚͑ͩͰྑ͍ʢ O(1) ʣ DPOUBJOFS CV⒎FS DPOUBJOFS NPWF
move ૢ࡞ͷίετ͕͘ͳ ͍࣌ std::array → ֤ཁૉͷ move ͔͠Ͱ͖ͳ͍ʢ O(n) ʣ
TUEBSSBZ CV⒎FS NPWF TUEBSSBZ CV⒎FS
move ૢ࡞ͷίετ͕͘ͳ ͍࣌ std::string →Small String Optimization • খ͍͞จࣈྻ
stack ʹऔΔ • 64bit OS, libc++ ͳΒ 24 จࣈ·Ͱ • move std::array ͷΑ͏ʹ 0(n) ͔͔Δ
move ૢ࡞͕͑ͳ͍࣌ ྫ֎҆શͳૢ࡞Λఏڙ͢Δίϯςφʢྫɿ std::vector ͷιʔτʣ → ཁૉͷ move ͕ྫ֎Λ ͛Δͱ͖
copy ʹ fallback • ཁૉͷ move தʹྫ֎͕ൃੜ͢Δͱ vector ͷ move ͕ྃͰ͖ͳ͍
move ͕༗ޮͰͳ͍࣌·ͱΊ • move ૢ࡞͕ଘࡏ͠ͳ͍ͱ͖ → ҉ͷ move ݅ݫ͍͠ •
move ૢ࡞͕͘ͳ͍ͱ͖ → std::array • move ૢ࡞͕͑ͳ͍ͱ͖ → std::vector ͷཁૉ͕ྫ֎҆શ ʹ move Ͱ͖ͳ͍࣌ • lvalue ͷͱ͖ → Ұ෦ͷྫ֎ʢ҉ͷ moveʣΛআ͍ͯ lvalue move Ͱ͖ͳ͍
Things to Remember • ςϯϓϨʔτΛ͍ͬͯΔ࣌ͲΜͳܕ͕ͬ ͯ͘Δ͔Θ͔Βͳ͍ → move ʹ͍ͭͯอक తʹίʔυΛॻ͘
• ѻ͏ܕͷৄࡉ͕͔͍ͬͯΔ → ܕͷυΩϡϝ ϯτΛಡΜͰ move ͕༗༻Ͱ͋ΕͦΕʹґ ଘͨ͠ίʔυΛॻ͍ͯྑ͍
Item 30: perfect forwarding ʹࣦഊ͢Δ߹ΛΖ͏
Perfect Forwarding Universal Reference Λ͍ɼؔͷҾΛ (const-ness ؚΊͯʣผͷؔݺͼग़͠ʹ ͦͷ··సૹ͢Δ 1
// perfect forward する関数 fwd 2 template<class... Ts> 3 auto fwd(Ts&& ... args) 4 { 5 // 関数 f に引数をそのまま転送する 6 return f(std::foward<Ts>(args)...); 7 } 8 9 ... 10 11 // 引数によっては perfect forwarding が失敗する 12 fwd({expression});
Perfect Forwarding ͕͏·͘ ͍͔ͳ͍Τοδέʔε ɾ{} ʹΑΔॳظԽ ɾ0 NULL ΛψϧϙΠϯλͱͯ͠͏
ɾએݴ͚ͩͷ static const ·ͨ constexpr σʔλϝϯό ɾΦʔόʔϩʔυ͞Εͨؔ ɾؔςϯϓϨʔτ ɾϏοτϑΟʔϧυ
{} ʹΑΔॳظԽ 1 void f(const std::vector<int>&); 2 3 ... 4
5 // OK. std::vector<int> は {1, 2, 3} で初期化される 6 f( {1, 2, 3} ); 7 8 // コンパイルエラー! 9 fwd( {1, 2, 3} ); foo.cpp:15:5: error: no matching function for call to 'fwd' fwd({1, 2, 3}); ^~~ ! foo.cpp:8:6: note: candidate function not viable: requires 0 arguments, but 1 was provided void fwd(Args &&... args) ^ 1 error generated.
{} ʹΑΔॳظԽ • f Λݺͼग़ͨ͠߹ɼίϯύΠϥ f ͷҾ ͔Β {1,2,3} Λ
vector ʹ҉ม͢Εྑ ͍͜ͱ͕͔Δ • fwd Λͬͯؒతʹݺͼग़ͨ͠߹ɼfwd ͷҾ Universal Reference ͷͨΊɼ{1,2,3} ΛͲͷܕʹม͢Εྑ͍ͷ͔͔Βͳ͍
ͳͥ std::initializer_list<int> ʹ ͳΒͳ͍ͷ͔ • Ҿ͕ std::initializer_list ͱ໌ࣔ͞Ε͍ͯͳ͍ ݶΓɼinitializer_list ʹਪ͞Εͳ͍ʢnon-
deduced contextʣ • ҎԼͷ߹ auto ͷਪ OK 1 // auto が std::initializer_list<int> に推論される 2 auto il = {1, 2, 3}; 3 4 // OK.すでに推論済み 5 fwd(il);
0 NULL ΛψϧϙΠϯλʹ ͬͨ߹ 0 NULL ܕʹਪ͞Εͯ͠·͏ͨΊ ϙΠϯλʹมͰ͖ͳ͍ʢItem8ʣˠ
nullptr Λ͑OK 1 void f(int *) 2 {} 3 4 template<class... Args> 5 void fwd(Args &&... args) 6 { 7 return f(std::forward<Args>(args)...); 8 } 9 10 fwd(NULL);
એݴ͚ͩͷ static const ·ͨ constexpr σʔλϝϯό Undefined symbols for architecture
x86_64: "Widget::MinVals", referenced from: _main in foo-4cf4cf.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) ! ※ clang 3.4 ͙Β͍·ͰίϯύΠϧ௨ͬͯ͠·͏ 1 struct Widget { 2 static constexpr int MinVals = 28; // 定義ではなく宣言 3 }; 4 5 void f(size_t val); 6 7 ... 8 9 f(Widget::MinVals); // OK 10 11 fwd(Widget::MinVals); // リンクエラー
એݴ͚ͩͷ static const ·ͨ constexpr σʔλϝϯό • f ͷ߹ MinVals
ͷ͕͠͞ΕΔͷͰ OK • fwd ͷ߹ɼUniversal Reference ࢀরͳͷ ͰɼMinVals ͷΞυϨε͕ඞཁʹͳΔ → Ϧϯ ΫΤϥʔ • ԼهͷΑ͏ʹ࣮ମΛఆ͓͚ٛͯ͠ OK const size_t Widget::MinVals;
Φʔόʔϩʔυ͞Εͨؔ foo.cpp:24:5: error: no matching function for call to 'fwd'
fwd(processVal); ^~~ foo.cpp:17:6: note: candidate function not viable: requires 0 arguments, but 1 was provided void fwd(Args &&... args) ^ 1 error generated. 1 int processVal(int val); 2 int processVal(int val, int priority); 3 4 void f(int (*fp)(int)); 5 6 ... 7 8 f(processVal); // OK 9 10 fwd(processVal); // コンパイルエラー!
Φʔόʔϩʔυ͞Εͨؔ • f ͷݺͼग़͠ͰҾͷܕ͔Β processVal ͷ ΦʔόʔϩʔυΛղܾͰ͖Δ • fwd ͷݺͼग़͠ͰҾ
Universal Reference ͷͨΊɼprocessVal ͷΦʔόʔ ϩʔυ͕ղܾͰ͖ͳ͍ • ԼهͷΑ͏ͳ߹ OK int (*p)(int) = processVal; // ͜͜ͰΦʔόʔϩʔυղܾࡁΈ fwd(p); // OK
ؔςϯϓϨʔτ Φʔόʔϩʔυͷ߹ͱಉ͡ 1 template<class T> 2 int processVal(T val);
3 4 void f(int (*fp)(int)); 5 6 ... 7 8 f(processVal); // OK 9 10 fwd(processVal); // T が解決できないため NG
ϏοτϑΟʔϧυ foo.cpp:25:9: error: non-const reference cannot bind to bit-field
'totalLength' fwd(h.totalLength); ^~~~~~~~~~~~~ foo.cpp:9:17: note: bit-field is declared here totalLength:16; ^ 1 error generated. 1 struct IPv4Header { 2 std::uint32_t version:4, 3 IHL:4, 4 DSCP:6, 5 ECN:2, 6 totalLength:16; 7 }; 8 9 void f(size_t s); 10 11 ... 12 13 IPv4Header h; 14 15 f(h.totalLength); // OK 16 fwd(h.totalLength); // コンパイルエラー!
ϏοτϑΟʔϧυ • ϏοτϑΟʔϧυͷΞΫηεඇconstࢀর → fwd ͷҾඇconstࢀরʹͳΔ → Ϗοτ ϑΟʔϧυඇconstࢀরʹଋറͰ͖ͳ͍ •
ಛఆͷϏοτϑΟʔϧυΛࢦ͢ϙΠϯλ͕ͭ ͘Εͳ͍ͷͱಉ͡ • ίϐʔΛͭ͘Ε OK 1 IPv4Header h; 2 3 auto length = static_cast<std::uint16_t>(totalLength); 4 5 fwd(length); // OK
·ͱΊ • Perfect Forwarding େͷ߹ࢥͬͨ௨ Γಈ͘ • ྫʹग़ͨΑ͏ͳكʹ͏·͍͔͘ͳ͍έʔε͕ ͋Δ •
ϫʔΫΞϥϯυΛ͓ͬͯ͘ͷ͕େࣄ
Things to Remember • Perfect Forwarding ࣦഊ͢Δ߹ • సૹؔͷҾͷܕͷਪʹࣦഊͨ࣌͠ •
సૹؔͷҾͷܕΛؒҧͬͨʢf ʹͤͳ͍ʣܕʹਪͯ͠͠·ͬͨ࣌ • ͦͷଞͷΤοδέʔε • {} ʹΑΔॳظԽ • 0 NULL ΛψϧϙΠϯλͱͯ͠͏ • એݴ͚ͩͷ static const ·ͨ constexpr σʔλϝϯό • Φʔόʔϩʔυ͞Εͨؔ • ؔςϯϓϨʔτ • ϏοτϑΟʔϧυ