名前空間
変種
操作

std::sub_match<BidirIt>::swap

From cppreference.com
< cpp‎ | regex‎ | sub match
 
 
 
正規表現ライブラリ
クラス
(C++11)
アルゴリズム
イテレータ
例外
Traits
定数
(C++11)
正規表現文法
 
 
void swap( sub_match& s ) noexcept(/* 以下参照 */);
(C++11以降)

2つのサブマッチオブジェクトの内容を交換します。以下と同等です。

this->pair<BidirIt, BidirIt>::swap(s);
std::swap(matched, s.matched);

目次

[編集] パラメータ

s - 交換するsub_match
型要件
-
BidirItLegacySwappable の要件を満たす必要があります。

[編集] 例外

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&) が追加されました。
English 日本語 中文(简体) 中文(繁體)