Namespaces
Variants

std::sub_match<BidirIt>:: swap

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
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 & )