std::sub_match<BidirIt>::swap
来自 cppreference.cn
void swap( sub_match& s ) noexcept(/* 见下方 */); |
(自 C++11 起) | |
交换两个 sub-match 对象的内容。等价于
this->pair<BidirIt, BidirIt>::swap(s);
std::swap(matched, s.matched);
目录 |
[edit] 参数
s | - | 要交换的 sub_match |
类型要求 | ||
-BidirIt 必须满足 LegacySwappable 的要求。 |
[edit] 异常
noexcept 规范:
noexcept(std::is_nothrow_swappable_v<BidirIt>)
[edit] 示例
运行此代码
#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() = []
[edit] 缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 3204 | C++11 | std::sub_match 使用了继承的 std::pair::swap(pair&) 这导致了切片 |
std::sub_match::swap(sub_match&) 被添加 |