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
680
C++20 射影変換
Akira Takahashi
June 20, 2025
Tweet
Share
More Decks by Akira Takahashi
See All by Akira Takahashi
P2P通信の標準化 WebRTCを知ろう
faithandbrave
6
2.7k
C++26アップデート 2025-03
faithandbrave
0
1.7k
C++26 エラー性動作
faithandbrave
2
1.2k
C++20の整数
faithandbrave
0
230
コンテナと文字列の中間インタフェースspanとstring_view
faithandbrave
1
580
C++23 スタックトレースライブラリ
faithandbrave
0
540
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
1.3k
使いたい標準C++機能がない環境でいかに実装・設計するか
faithandbrave
2
1.2k
C++20からC++23までの変化
faithandbrave
9
12k
Other Decks in Programming
See All in Programming
高度なUI/UXこそHotwireで作ろう Kaigi on Rails 2025
naofumi
4
3.7k
どの様にAIエージェントと 協業すべきだったのか?
takefumiyoshii
2
630
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
780
止められない医療アプリ、そっと Swift 6 へ
medley
1
140
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
2
750
詳しくない分野でのVibe Codingで困ったことと学び/vibe-coding-in-unfamiliar-area
shibayu36
3
4.7k
Signals & Resource API in Angular: 3 Effective Rules for Your Architecture @BASTA 2025 in Mainz
manfredsteyer
PRO
0
110
iOS 17で追加されたSubscriptionStoreView を利用して5分でサブスク実装チャレンジ
natmark
0
660
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
450
ポスターセッション: 「まっすぐ行って、右!」って言ってラズパイカーを動かしたい 〜生成AI × Raspberry Pi Pico × Gradioの試作メモ〜
komofr
0
1.2k
Go言語の特性を活かした公式MCP SDKの設計
hond0413
1
200
Cloudflare AgentsとAI SDKでAIエージェントを作ってみた
briete
0
130
Featured
See All Featured
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
5.6k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
Build your cross-platform service in a week with App Engine
jlugia
232
18k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
20k
Designing for Performance
lara
610
69k
Side Projects
sachag
455
43k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.5k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.2k
Agile that works and the tools we love
rasmusluckow
331
21k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.1k
Practical Orchestrator
shlominoach
190
11k
How STYLIGHT went responsive
nonsquared
100
5.8k
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が 使われているかで確認できる
今回は以上です! • 射影変換は、いくつかの状況でコードをより単純化で きます • 複雑な条件が必要ない状況で便利に使えます