int a, int b) { [[maybe_unused]] const int c = (a + b); assert(c > 1); walk(); } }; オーバーライドしたメンバ関数で使わない引数や、assert 内でしか使わない変数は、 変数未使用の警告の要因になる。未使用でも問題ないことをコンパイラに伝える [[maybe_unused]] 属性を付けることで警告を抑制できる。 29 リリースビルドでは c が使われなくなる
int main() { vector<string> ss = { ... }; size_t i = 0; for (const auto& s : ss) cout << i++ << ": " << s << '\n'; // ループのあとでも i が使えてしまう } int main() { vector<string> ss = { ... }; for (size_t i = 0; const auto& s : ss) cout << i++ << ": " << s << '\n'; // i のスコープはループ内のみ } 50
void work(unique_ptr<int> ptr, int n) { bool flag = (n > 3); auto f = [this, // メンバ変数はキャプチャできないので代わりに this をキャプチャ ptr, // move した結果をキャプチャできないのでエラー flag // キャプチャするものはローカル変数として一旦作らないといけない ]() mutable { ... }; } }; int d = 5; auto n = count_if(v.begin(), v.end(), [d](int n){ return n % d == 0; }); 61
work(unique_ptr<int> ptr, int n) { auto f = [&b = m_buffer, // 新しい参照変数 b でメンバ変数を参照キャプチャ p = move(ptr), // 新しい変数 p を作ってムーブキャプチャ flag = (n > 3) // 新しい変数 flag で式の結果をキャプチャ ]() mutable { ... }; } }; 62
• 実行時性能が向上する機能 • 今後に期待な機能 ② C++ の仕様はどのように決まる? • ゲーム開発と C++ 標準化 • C++ に Issue を送ってみた体験談 • 国内 WG 委員インタビュー • ゲーム開発に関連する、議論が進行中の提案 • 最新の C++ 情報にキャッチアップ 135
することで、自分たちにとって使いやすいプログラミング言語にしていくことができる。 ◆ C++ の仕様とゲーム開発 ゲーム開発に関係する C++ の言語や標準ライブラリ機能 (ネットワーク、グラフィッ クス、オーディオ等) が他国主導で決められると、日本のゲームハードウェアの性能を 生かしきれない場合もあり、独自 API 乱立でゲーム開発のハードルが上がる原因に。 C++ の新しい 標準機能です! このハードで使うと問題が あるので使用禁止です。 136
Add shift to <algorithm> I. Introduction This paper proposes adding shift algorithms to the C++ STL which move elements forward or backward in a range of elements. 提案のタイトル 文書番号 + リビジョン番号 提出日 著者 議論を行うワーキンググループ名 概要の説明 143
a range is a basic operation which the STL should allow performing easily. An important use case is time series analysis algorithms used in scientific and financial applications. The scope of the proposal is adding the following function III. Impact on the Standard The only impact on the standard is adding the proposed function templates to <algorithm>. IV. Design Decisions 1) shift_left and shift_right are provided as separate function templates instead of just a single shift function template to maximize performance and minimize compiled code size. Since shifting left and shifting right may have significantly different implementations (as is the case in the sample implementation), implementing both shift directions in a single shift 提案が必要な理由 提案が既存の規格に 及ぼす影響 代替手法と比較して 優れる理由 2/3 144
shuffle, add: // [<link to alg.shift>], shift template<class ForwardIterator> constexpr ForwardIterator shift_left( ForwardIterator first, ForwardIterator last, typename iterator_traits<ForwardIterator>::difference_type n ); VII. Revision History R2 - Removed unneeded MoveConstructible requirements. - Make shift_left constexpr. - Improved wording. - Removed extra specification in the proposal text about the order in which VIII. Acknowledgements IX. References 詳しい書き方: wg21.link/n3370 既存の規格書からの差分。 加筆, 削除 リビジョンの変更履歴 謝辞 参考文献 3/3 145
C++20 現在、構造化束縛で値を無視することはできません。現在アクティブな提案文 書 P2169R1: [Evolution] A Nice Placeholder With No Name は、アンダースコアの記 号 _ を、汎用的なプレースホルダーとして導入することを提案していて、その用例とし て、構造化束縛での不要な値の無視を挙げています。提案はまだ初期段階で、コア言語の 変更でもあるため、進展には時間がかかると思われます。 160