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
420
明日から使える fold expression テクニック
歌舞伎座.tech 番外編「江添亮の詳説C++17」出版記念で発表したLTの資料
白山風露
March 14, 2018
Tweet
Share
Other Decks in Programming
See All in Programming
Go1.22からの疑似乱数生成器について/go-122-pseudo-random-generator
convto
1
160
受託開発でGitLab CI を活用していく
xiombatsg
1
130
DDD, necessary but insufficient: physical design principles for microservices
cer
PRO
0
2.2k
複雑なドメインを扱うプロダクトの探索フェーズではいつどのようにテストをするのか / How to testing during exploratory phase
boykush
2
150
GitHub Copilot Tips and Tricks
yuichielectric
26
7.5k
Open Source Swiftc Workshop
kitasuke
1
290
Kotlinを用いたDSL的な設計手法と使用上の注意
kohii00
3
530
CSRF対策のやり方、そろそろアップデートしませんか / Update your knowledge of CSRF protection
hiro_y
25
15k
自作ソフト(VMagicMirror)がVRMA対応してる話+実装のTips
bakudreameater
0
110
オブジェクトしこう
okuramasafumi
2
130
Data Contracts In Practice With Debezium and Apache Flink (Kafka Summit London)
gunnarmorling
2
280
Cloud RunとCloud PubSubでサーバレスなデータ基盤2024 with Terraform / Cloud Run and PubSub with Terraform
shinyorke
7
1.9k
Featured
See All Featured
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
11
1.4k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
Building Applications with DynamoDB
mza
88
5.6k
BBQ
matthewcrist
78
8.7k
Git: the NoSQL Database
bkeepers
PRO
421
63k
It's Worth the Effort
3n
180
27k
Statistics for Hackers
jakevdp
789
220k
Bash Introduction
62gerente
604
210k
WebSockets: Embracing the real-time Web
robhawkes
59
6.9k
Done Done
chrislema
178
15k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
19
1.6k
GitHub's CSS Performance
jonrohan
1023
450k
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