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
C++20 射影変換
Search
Akira Takahashi
June 20, 2025
Programming
780
0
Share
C++20 射影変換
Akira Takahashi
June 20, 2025
More Decks by Akira Takahashi
See All by Akira Takahashi
P2P通信の標準化 WebRTCを知ろう
faithandbrave
6
3.1k
C++26アップデート 2025-03
faithandbrave
0
2.1k
C++26 エラー性動作
faithandbrave
2
1.2k
C++20の整数
faithandbrave
0
280
コンテナと文字列の中間インタフェースspanとstring_view
faithandbrave
1
630
C++23 スタックトレースライブラリ
faithandbrave
0
620
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
1.5k
使いたい標準C++機能がない環境でいかに実装・設計するか
faithandbrave
2
1.3k
C++20からC++23までの変化
faithandbrave
9
12k
Other Decks in Programming
See All in Programming
Vibe NLP for Applied NLP
inesmontani
PRO
0
570
書籍「ユーザーストーリーマッピング」が私のバイブル
asumikam
4
460
なぜあなたのコードには「コシ」がないのか?〜AI時代に問う、最後まで美味しい設計と戦略〜 #phpconkagawa / phpconkagawa2026
shogogg
0
110
アクセシビリティ試験の"その後"を仕組み化する
yuuumiravy
1
190
GNU Makeの使い方 / How to use GNU Make
kaityo256
PRO
16
5.6k
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
270
Programming with a DJ Controller — not vibe coding
m_seki
3
740
의존성 주입과 모듈화
fornewid
0
160
20年以上続くプロダクトでも使い続けられる静的解析ツールを求めて
matsuo_atsushi
0
120
実践CRDT
tamadeveloper
0
610
【26新卒研修】OpenAPI/Swagger REST API研修
dip_tech
PRO
0
130
CDK Deployのための ”反響定位”
watany
5
920
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
900
Practical Orchestrator
shlominoach
191
11k
Java REST API Framework Comparison - PWX 2021
mraible
34
9.3k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.9k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
New Earth Scene 8
popppiees
3
2.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
170
Building the Perfect Custom Keyboard
takai
2
740
Measuring & Analyzing Core Web Vitals
bluesmoon
9
820
Producing Creativity
orderedlist
PRO
348
40k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
510
Transcript
C++20 射影変換 高橋 晶 (Akira Takahashi)
[email protected]
Preferred Networks, Inc.
2025/06/20 (金) C++ breaktime 2025 / Summer
自己紹介 • Preferred Networks社で、スーパーコンピュータMN-Coreの ソフトウェアを作っています • エミュレータとかアセンブラとかの低レイヤーなものを作ってます • 通信系のゲームエンジンを作ってます •
C++の日本語リファレンスサイトcpprefjpを作っています • 著書 • 『C++テンプレートテクニック』 • 『C++ポケットリファレンス』 • 『プログラミングの魔導書』 • 東京で2〜3ヶ月ごとにC++ MIXという勉強会を開催しています
C++20ではアルゴリズムに射影変換という機能が 追加されています • 最近まで知りませんでした • Range関係でいっしょに入った機能なので埋もれていました
アルゴリズムおさらい • C++標準のアルゴリズムは、コンテナ と分離され、イテレータという中間イン タフェースを介して各要素にアクセスす る // コンテナはもちろん、 vector<T> v;
auto it = find( v.begin(), v.end(), x); // 組み込み配列にも使える T ar[N]; auto p = find( ar, ar + N, x );
アルゴリズムおさらい • C++20ではRangeに対応した // コンテナはもちろん、 vector<T> v; auto it =
ranges::find( v, x); // 組み込み配列にも使える T ar[N]; auto p = ranges::find( ar, x );
findとかcountってじつは不便 • 特定の値を検索するfind • 特定の値の個数を取得するcount • これらはそんなに活躍しない • コンテナの要素型や検索条件は そんなに単純ではないから
vector<T> v; auto it = ranges::find( v, x); int n = ranges::count( ar, x );
findとかcountってじつは不便 • より細かい条件を設定できるfind_if / count_ifが使われがち vector<T> v; auto it =
ranges::find_if( v, [](T x) { return x > 0; }); int n = ranges::count_if( ar, [](T x) { return x.kind == Weapon; } );
C++20 射影変換で値を変換できるようになった • より細かい条件を設定できるfind_if / count_ifが使われがち • C++20では射影変換 (projection) と
いう機能が入り、値を変換した結果 で検索ができるようになった • メンバ変数ポインタ、メンバ関数 ポインタ、関数オブジェクトなどを 指定できる vector<T> v; auto it = ranges::find( v, x, // xはnameの型 &T::name); // nameの値を検索 int n = ranges::count( ar, Weapon, [](T x) { return x.kind; } );
変換時の値コピーに注意 • メンバポインタならパフォーマンス 上の問題はないが、 • ラムダ式を指定する場合、 パフォーマンス劣化に注意 • 戻り値の型を指定して、参照を返す ようにしよう
vector<T> v; auto it = ranges::find( v, x, // コピーされないよう型を指定 [](T x) -> const string& { return x.name; } );
変換時の値コピーに注意 • decltype(auto)でもOK • return文にカッコをつけないとコ ピーになるので注意 vector<T> v; auto it
= ranges::find( v, x, // コピーされないよう型を指定 [](T x) -> decltype(auto) { return (x.name); } );
射影変換がサポートされているアルゴリズムか確認 template <input_range R, class Proj = identity, class T
= projected_value_t<iterator_t<R>, Proj>> constexpr borrowed_iterator_t<R> find(R&& r, const T& value, Proj proj = {}); • アルゴリズムが射影変換に対応しているかは、 パラメータに「Proj proj」があるかで確認できる • パラメータのどの値が変換されるかは、projected_value_tが 使われているかで確認できる
今回は以上です! • 射影変換は、いくつかの状況でコードをより単純化で きます • 複雑な条件が必要ない状況で便利に使えます