std::sub_match<BidirIt>::swap
来自 cppreference.cn
void swap( sub_match& s ) noexcept(/* 参见下方 */); |
(C++11 起) | |
交换两个子匹配对象的内容。等价于
this->pair<BidirIt, BidirIt>::swap(s);
std::swap(matched, s.matched);
目录 |
[编辑] 参数
s | - | 要交换的 sub_match |
类型要求 | ||
-BidirIt 必须满足 LegacySwappable 的要求。 |
[编辑] 异常
noexcept 规范:
noexcept(std::is_nothrow_swappable_v<BidirIt>)
[编辑] 示例
运行此代码
#include <cassert> #include <iostream> #include <regex> int main() { const char* s = "Quick red cat"; std::sub_match<const char*> x, y; x.first = &s[0]; x.second = &s[5]; x.matched = false; y.first = &s[012]; y.second = &s[13]; y.matched = true; std::cout << "Before swap:\n"; std::cout << "x.str() = [" << x.str() << "]\n"; std::cout << "y.str() = [" << y.str() << "]\n"; assert(!x.matched and y.matched); x.swap(y); std::cout << "After swap:\n"; std::cout << "x.str() = [" << x.str() << "]\n"; std::cout << "y.str() = [" << y.str() << "]\n"; assert(x.matched and !y.matched); }
输出
Before swap: x.str() = [] y.str() = [cat] After swap: x.str() = [cat] y.str() = []
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 3204 | C++11 | std::sub_match 使用继承的 std::pair::swap(pair&) 导致切片 |
添加了 std::sub_match::swap(sub_match&) |