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++26アップデート 2025-03
Search
Akira Takahashi
April 25, 2025
Technology
0
1.3k
C++26アップデート 2025-03
Akira Takahashi
April 25, 2025
Tweet
Share
More Decks by Akira Takahashi
See All by Akira Takahashi
C++26 エラー性動作
faithandbrave
2
1.1k
C++20の整数
faithandbrave
0
180
コンテナと文字列の中間インタフェースspanとstring_view
faithandbrave
1
470
C++23 スタックトレースライブラリ
faithandbrave
0
450
if constexpr文はテンプレート世界のラムダ式である
faithandbrave
3
1.2k
使いたい標準C++機能がない環境でいかに実装・設計するか
faithandbrave
2
1.1k
C++20からC++23までの変化
faithandbrave
9
12k
オープン化が進むC++の現状と展望
faithandbrave
19
11k
C++20 status
faithandbrave
0
950
Other Decks in Technology
See All in Technology
Coding Agentに値札を付けろ
watany
3
540
Global Azure2025(GitHub Copilot ハンズオン)
tomokusaba
2
810
計測による継続的なCI/CDの改善
sansantech
PRO
7
1.5k
激動の一年を通じて見えてきた「技術でリードする」ということ
ktr_0731
8
7.5k
TanStack Start 技術選定の裏側 / Findy-Lunch-LT-TanStack-Start
iktakahiro
1
160
20250514 1Passwordを使い倒す道場 vol.1
east_takumi
0
130
クラウドネイティブ環境の脅威モデリング
kyohmizu
2
430
250510 StepFunctionのテスト自動化始めました vol.1
east_takumi
1
250
encoding/json v2を予習しよう!
yuyu_hf
PRO
1
200
本当に必要なのは「QAという技術」だった!試行錯誤から生まれた、品質とデリバリーの両取りアプローチ / Turns Out, "QA as a Discipline" Was the Key!
ar_tama
9
4.7k
Part2 GitHub Copilotってなんだろう
tomokusaba
2
840
製造業向けIoTソリューション提案資料.pdf
haruki_uiru
0
280
Featured
See All Featured
Building an army of robots
kneath
305
45k
Six Lessons from altMBA
skipperchong
28
3.8k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Scaling GitHub
holman
459
140k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
800
What's in a price? How to price your products and services
michaelherold
245
12k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
41
2.3k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Balancing Empowerment & Direction
lara
0
21
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
13
850
Transcript
C++26 アップデート 2025-03 高橋 晶 (Akira Takahashi)
[email protected]
Preferred Networks,
Inc. 2025/04/25 (金) C++ MIX #14
C++26 2025-03 • 今回は、2025-03 mailingとしてC++26 Working Draftに入った 変更を紹介していきます • 今回でC++26仕様は一旦できたらしい
• このあとは仕様バグの修正や、各国からのコメントへの対応などがあ る
constexpr unionによる遅延初期化 • 配列要素の初期化を遅延させるための手法 としてunionが使われていたが、constexpr をつけるとコンパイラによって動かなかっ たりした • C++26では明確に許可される template
<class T, int N> class FixedVector { union U { constexpr U() { } constexpr ~U() { } T storage[N]; }; size_t size = 0; … };
タイムトラベル最適化を防止するstd::observable() • DIEを定義しない場合、inc(nullptr)は未 定義動作になることが保証されるため、 bad()をそもそも呼び出さないタイムト ラベル最適化が行われる • C++26ではstd::observable()という関数 でチェックポイントを設定すると、 時間分割されてチェックポイントを跨い
だタイムトラベル最適化が防止される • C++26の標準出力関係にはチェックポ イントが設定される static void bad(const char *msg) { std::fputs(msg, stderr); #ifdef DIE std::abort(); #endif } void inc(int *p) { if(!p) bad("Null! n"); ++*p; // !p時にここで未定義動作 }
契約プログラミング • 3つの契約機能 • 関数の事前条件pre • 関数の事後条件post • 契約アサーションcontract_assert •
postは戻り値に変数名をつけて使用 してもいいし、使用しなくてもいい • pre/postは文脈依存キーワード (変数名とかに使える) • コンパイラに契約モードの設定が追 加される想定 int f(const int x) pre (x != 1) post (r: r == x && r != 2) { contract_assert(x != 3); return x; } void clear() post (empty()) { … }
memory_order::consume関係を非推奨化 • consumeメモリオーダーは、並行プログラムにおいて、 読み込んだ値に依存した操作の実行順序を保証するもの • 実際はconsume相当の順序保証はどのメモリオーダーにも付くのと、 コンパイラがconsume特化の実装をしなかったので非推奨化する • 関連してstd::kill_dependency()と[[carries_dependency]]も非推奨化 std::atomic<T>
x = …; int a = x.load(memory_order::consume); // 非推奨 int b = a + 1; int c = b + 1;
コンセプトと変数テンプレートの テンプレートテンプレートパラメータ対応 • コンセプトと変数テンプレート に、テンプレートテンプレート パラメータが使えるようになる • テンプレートテンプレートパラ メータは、テンプレート引数を あとで指定する機能
template< template <typename T> concept C, template <typename T> auto V > struct S { concept D = C<int>; constexpr auto X = V<int>; }; template <typename T> concept Concept = true; template <typename T> constexpr auto Var = 42; S<Concept, Var> s;
トリビアルな再配置 • trivially relocatable (トリビアルな再 配置) は、memcpyやビットコピーで 再配置ができる性質 • 型にこの性質を与えることで、ムーブ
元オブジェクトのデストラクタ呼び出 しなどを省略できる • そのために文脈依存キーワードが2つ 追加される (それと対応した型特性も) template <class T> class optional trivially_relocatable_if_eligible replaceable_if_eligible { union { T d_object; }; bool d_engaged{false}; … };
#embed : プリプロセス時ファイル読み込み • #includeのように使える ファイル読み込み機能 • 上限のバイト数や、先頭列、末尾列、 空であった場合のデータなどを指定 できる
// 無限サイズのファイル"dev/urandom" // から最大4バイトを読む const unsigned char random[] = { #embed "/dev/urandom" limit(4) }; constexpr int seed = 0; for (int i = 0; i < 4; i++) { seed |= random[i] << (i * 8); } constexpr std::mt19937 gen{seed};
vector / string以外のコンテナをconstexpr対応 • C++20でstd::vectorとstd::stringが constexprで使えるようになった • C++26では、ほかの標準コンテナも すべてconstexpr対応する constexpr
R f() { std::map<std::string, int> m = { … }; if (m.contains("key")) { … } … }
hive : 要素のメモリ位置が安定するシーケンスコンテナ • 双方向Rangeのシーケンスコンテナ std::hiveが追加される • 要素の追加・削除をしても、メモリ位 置は変わらない •
ゲーム、HPC、物理シミュレーション など幅広い分野で使われてきたデータ 構造 std::hive<int> ls = {1, 2, 3}; // 要素を追加。 // {4, 5}の新たなメモリブロックが確保される // {1, 2, 3}のメモリ位置は変わらない ls.insert({4, 5}); // 要素3を削除。 // {4, 5}のメモリ位置は変わらない ls.erase(std::next(ls.begin(), 2));
indirectとpolymorphic : 値の意味論をもつ動的確保オブジェクト • indirectとpolymorphicは、動的確保し たオブジェクトのポインタに、値の意 味論をもたせるもの • コピー時にポインタのコピーではなく、 参照先の値をコピー
(ディープコピー) する • pImplイディオムもunique_ptrの変わり にindirectを使える class ForwardListNode { int data_; indirect<ForwardListNode> next_; }; class ForwardList { indirect<ForwardListNode> head_; }; class Composite { indirect<A> a_; polymorphic<X> x_; public: template <class Derived> Composite(const A& a, const Derived& x) : a_{a}, x_{std::in_place_type<Derived>, x} {} };
今回は以上です! • C++26のアップデートや、 それぞれの機能を掘り下げる発表は 今後もやっていきます