void swap(sub_match& s) noexcept(see below); // C++23
概要
別のsub_matchオブジェクトとデータを交換する。
事前条件
BidirectionalIteratorがSwappableの要件を満たすこと。
効果
以下と等価:
例外
この関数の例外仕様は、is_nothrow_swappable_v<BidirectionalIterator>と等価である。
備考
- このメンバ関数はC++23に対する欠陥報告 (LWG 3204) として追加されたものであり、コンパイラは早期に対応している場合がある。そのため、C++20モードでも使用できる可能性がある。
例
#include <iostream>
#include <regex>
#include <string>
int main()
{
const std::string s(" abc 123 ");
const std::regex re(R"((\w+)\s+(\d+))");
std::smatch m;
std::regex_search(s, m, re);
std::ssub_match a = m[1]; // "abc"
std::ssub_match b = m[2]; // "123"
a.swap(b);
std::cout << a.str() << std::endl;
std::cout << b.str() << std::endl;
}
出力
123
abc
バージョン
言語
- C++23
処理系
- Clang: 17 ✅
- GCC: 13.1 ✅
- Visual C++: 2022 Update 7 ✅
関連項目
参照
- LWG Issue 3204.
sub_match::swaponly swaps the base class- C++23で、基底クラス
pairから継承したswapはメンバ変数matchedを交換しなかったため、matchedも交換する専用のメンバ関数swapが追加された
- C++23で、基底クラス