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 ( /* see below */ ) ;
(C++11以降)

2つの部分一致オブジェクトの内容を交換します。以下と同等です:

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++規格に対して遡及的に適用されました。

DR 適用バージョン 公開時の動作 正しい動作
LWG 3204 C++11 std::sub_match が継承した std :: pair :: swap ( pair & )
を使用しており、スライシングが発生していた
std :: sub_match :: swap ( sub_match & ) が追加された