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
Basic Architectures
denyspoltorak
0
120
Developing static sites with Ruby
okuramasafumi
0
330
リリース時」テストから「デイリー実行」へ!開発マネージャが取り組んだ、レガシー自動テストのモダン化戦略
goataka
0
140
Flutter On-device AI로 완성하는 오프라인 앱, 박제창 @DevFest INCHEON 2025
itsmedreamwalker
1
160
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
170
クラウドに依存しないS3を使った開発術
simesaba80
0
170
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
4
1.3k
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
390
20251212 AI 時代的 Legacy Code 營救術 2025 WebConf
mouson
0
220
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
640
Grafana:建立系統全知視角的捷徑
blueswen
0
230
GISエンジニアから見たLINKSデータ
nokonoko1203
0
180
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
110
Chasing Engaging Ingredients in Design
codingconduct
0
85
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
400
Evolving SEO for Evolving Search Engines
ryanjones
0
76
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
130
Building Flexible Design Systems
yeseniaperezcruz
330
39k
Typedesign – Prime Four
hannesfritz
42
2.9k
Optimizing for Happiness
mojombo
379
70k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
0
170
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Why Our Code Smells
bkeepers
PRO
340
57k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
2.8k
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