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
ProxyによるWindow間RPC機構の構築
syumai
3
1.2k
How Android Uses Data Structures Behind The Scenes
l2hyunwoo
0
480
Rancher と Terraform
fufuhu
2
550
Navigation 2 を 3 に移行する(予定)ためにやったこと
yokomii
0
350
rage against annotate_predecessor
junk0612
0
170
個人開発で徳島大学生60%以上の心を掴んだアプリ、そして手放した話
akidon0000
1
150
Compose Multiplatform × AI で作る、次世代アプリ開発支援ツールの設計と実装
thagikura
0
170
Amazon RDS 向けに提供されている MCP Server と仕組みを調べてみた/jawsug-okayama-2025-aurora-mcp
takahashiikki
1
120
個人軟體時代
ethanhuang13
0
330
AI Coding Agentのセキュリティリスク:PRの自己承認とメルカリの対策
s3h
0
240
アルテニア コンサル/ITエンジニア向け 採用ピッチ資料
altenir
0
110
Putting The Genie in the Bottle - A Crash Course on running LLMs on Android
iurysza
0
140
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
368
19k
Six Lessons from altMBA
skipperchong
28
4k
Designing for humans not robots
tammielis
253
25k
Become a Pro
speakerdeck
PRO
29
5.5k
The Invisible Side of Design
smashingmag
301
51k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Navigating Team Friction
lara
189
15k
What's in a price? How to price your products and services
michaelherold
246
12k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
8
930
YesSQL, Process and Tooling at Scale
rocio
173
14k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Into the Great Unknown - MozCon
thekraken
40
2k
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