std::sub_match<BidirIt>::swap

来自cppreference.com
< cpp‎ | regex‎ | sub match
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 << "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 << "swap 后:\n";
    std::cout << "x.str() = [" << x.str() << "]\n";
    std::cout << "y.str() = [" << y.str() << "]\n";
    assert(x.matched and !y.matched);
}

输出:

swap 前:
x.str() = []
y.str() = [cat]
swap 后:
x.str() = [cat]
y.str() = []

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告应用于出版时的行为正确行为
LWG 3204C++11std::sub_match 曾继承于 std::pair::swap(pair&) 而导致切片添加 std::sub_match::swap(sub_match&)