Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
140
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
1
240
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
6
3.7k
ローターアクトEクラブ アメリカンナイト:川端 柚菜 氏(Japan O.K. ローターアクトEクラブ 会長):2720 Japan O.K. ロータリーEクラブ2025年12月1日卓話
2720japanoke
0
730
MAP, Jigsaw, Code Golf 振り返り会 by 関東Kaggler会|Jigsaw 15th Solution
hasibirok0
0
250
非同期処理の迷宮を抜ける: 初学者がつまづく構造的な原因
pd1xx
1
730
WebRTC と Rust と8K 60fps
tnoho
2
2k
re:Invent 2025 のイケてるサービスを紹介する
maroon1st
0
130
開発に寄りそう自動テストの実現
goyoki
2
1.1k
FluorTracer / RayTracingCamp11
kugimasa
0
240
AtCoder Conference 2025「LLM時代のAHC」
imjk
2
520
新卒エンジニアのプルリクエスト with AI駆動
fukunaga2025
0
230
Featured
See All Featured
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.7k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.3k
A better future with KSS
kneath
240
18k
The Language of Interfaces
destraynor
162
25k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Docker and Python
trallard
47
3.7k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.1k
Producing Creativity
orderedlist
PRO
348
40k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
Six Lessons from altMBA
skipperchong
29
4.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
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