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
明日から使える fold expression テクニック
Search
白山風露
March 14, 2018
Programming
1
440
明日から使える fold expression テクニック
歌舞伎座.tech 番外編「江添亮の詳説C++17」出版記念で発表したLTの資料
白山風露
March 14, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
Android 16KBページサイズ対応をはじめからていねいに
mine2424
0
240
ISUCON研修おかわり会 講義スライド
arfes0e2b3c
1
460
PicoRuby on Rails
makicamel
2
140
状態遷移図を書こう / Sequence Chart vs State Diagram
orgachem
PRO
2
170
「テストは愚直&&網羅的に書くほどよい」という誤解 / Test Smarter, Not Harder
munetoshi
0
190
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
650
Composerが「依存解決」のためにどんな工夫をしているか #phpcon
o0h
PRO
1
330
NEWT Backend Evolution
xpromx
1
110
Goで作る、開発・CI環境
sin392
0
260
The Niche of CDK Grant オブジェクトって何者?/the-niche-of-cdk-what-isgrant-object
hassaku63
1
500
脱Riverpod?fqueryで考える、TanStack Queryライクなアーキテクチャの可能性
ostk0069
0
340
猫と暮らす Google Nest Cam生活🐈 / WebRTC with Google Nest Cam
yutailang0119
0
160
Featured
See All Featured
What's in a price? How to price your products and services
michaelherold
246
12k
Building a Modern Day E-commerce SEO Strategy
aleyda
42
7.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
3.9k
Raft: Consensus for Rubyists
vanstee
140
7k
jQuery: Nuts, Bolts and Bling
dougneiner
63
7.8k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.8k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
Building Adaptive Systems
keathley
43
2.7k
Transcript
明日から使える fold expression テクニック
明日から使えるの? • GCC => 6 • Clang => 3.6 •
MSVC => 19.12 (Visual Studio 15.5) http://en.cppreference.com/w/cpp/compiler_support 調べ →明日から使える!
Fold expressionとは?
Fold expressionとは? • C++17で追加された新機能 • パラメーターパックを演算子で結合して展開できる • 別の関数を用意しなくても良くなる
Fold expression の文法 Unary fold expression と Binary fold expression
がある
Fold expression の文法 Unary fold expression “exp” がパラメーターパックを含む式、 ”@” を何らかの二項演算子として、
(exp @ ...) // (1) (... @ exp) // (2)
Fold expression の文法 Unary fold expression 長さnのパラメーターパックのk番目の項を含む式をexp k として、 exp
1 @ (exp 2 @ (... @ (exp n-2 @ exp n-1 ))) // (1) (((exp 1 @ exp 2 ) @ ...) @ exp n-2 ) @ exp n-1 // (2) と展開される。
Fold expression の文法 Unary fold expression パラメーターパックが空のとき、以下の演算子に関してはデフォルトの値が存在する。 • && →
true • || → false • , → void() それ以外の演算子の場合、パラメーターパックが空だとエラーになる。
Fold expression の文法 Binary fold expression “exp” がパラメーターパックを含む式、 ”init” を任意の式、”@”
を何らかの二項演算子として、 (exp @ ... @ init) // (1) (init @ ... @ exp) // (2)
Fold expression の文法 Binary fold expression 長さnのパラメーターパックのk番目の項を含む式をexp k として、 exp
1 @ (exp 2 @ (... @ (exp n-2 @ (exp n-1 @ init)))) // (1) ((((init @ exp 1 ) @ exp 2 ) @ ...) @ exp n-2 ) @ exp n-1 // (2) と展開される。
Fold expression の文法 これは fold expression ではない func(exp...); return {
exp... };
1.複数の真偽値の論理和や論理積を取る template<bool ...args> inline constexpr auto conjunction = (args &&
...); template<bool ...args> inline constexpr auto disjunction = (args || ...);
1.複数の真偽値の論理和や論理積を取る constexpr auto conjunction = [](std::initializer_list<bool> init) { bool result
= true; for (bool v : init) { result &= v; if (!v) return false; } return result; };
2.一致する型を数える template<typename T, typename ...Args> inline constexpr auto type_count =
(std::size_t{} + ... + std::is_same_v<T, Args>);
template<typename T, typename ...Args> inline constexpr auto type_index_of = []{
std::size_t i = 0; ((!std::is_same_v<T, Args> && ++i) && ...); return i; }(); 3.先頭から一致する型の位置を探す
template<typename T, std::size_t index, typename ...Args> inline constexpr auto type_index_of
= []{ std::size_t i = 0; (((i < index || !std::is_same_v<T, Args>) && ++i) && ...); return i; }(); 4.途中から一致する型の位置を探す
inline constexpr auto call = [](auto&& func, auto&& ...args) {
(void(func(std::forward<decltype(args)>(args))), ...); }; 5.全ての値に関して関数を呼び出す
君も明日からfold expressionを使おう!
自己紹介 名前:吉田大樹 仕事:プログラマー。電子書籍リーダーの開発。最近は業務では C++よりもTypeScriptやってる twitter: @kazatsuyu