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
0
500
C++20 射影変換
Akira Takahashi
June 20, 2025
Tweet
Share
More Decks by Akira Takahashi
See All by Akira Takahashi
C++26アップデート 2025-03
faithandbrave
0
1.4k
C++26 エラー性動作
faithandbrave
2
1.1k
C++20の整数
faithandbrave
0
200
コンテナと文字列の中間インタフェースspanとstring_view
faithandbrave
1
500
C++23 スタックトレースライブラリ
faithandbrave
0
470
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
1.2k
使いたい標準C++機能がない環境でいかに実装・設計するか
faithandbrave
2
1.1k
C++20からC++23までの変化
faithandbrave
9
12k
オープン化が進むC++の現状と展望
faithandbrave
19
11k
Other Decks in Programming
See All in Programming
データベースコネクションプール(DBCP)の変遷と理解
fujikawa8
1
270
統一感のある Go コードを生成 AI の力で手にいれる
otakakot
0
3k
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
22
6.2k
第9回 情シス転職ミートアップ 株式会社IVRy(アイブリー)の紹介
ivry_presentationmaterials
1
190
アンドパッドの Go 勉強会「 gopher 会」とその内容の紹介
andpad
0
250
GraphRAGの仕組みまるわかり
tosuri13
7
450
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
920
Cursor AI Agentと伴走する アプリケーションの高速リプレイス
daisuketakeda
1
120
Gleamという選択肢
comamoca
6
740
たった 1 枚の PHP ファイルで実装する MCP サーバ / MCP Server with Vanilla PHP
okashoi
0
120
エンジニア向け採用ピッチ資料
inusan
0
140
Javaに鉄道指向プログラミング (Railway Oriented Pro gramming) のエッセンスを取り入れる/Bringing the Essence of Railway-Oriented Programming to Java
cocet33000
2
580
Featured
See All Featured
Scaling GitHub
holman
459
140k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
45
7.4k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
357
30k
It's Worth the Effort
3n
184
28k
Embracing the Ebb and Flow
colly
86
4.7k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
GitHub's CSS Performance
jonrohan
1031
460k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
181
53k
VelocityConf: Rendering Performance Case Studies
addyosmani
330
24k
Agile that works and the tools we love
rasmusluckow
329
21k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
700
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が 使われているかで確認できる
今回は以上です! • 射影変換は、いくつかの状況でコードをより単純化で きます • 複雑な条件が必要ない状況で便利に使えます