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)

Обменивает содержимое двух объектов под-совпадений. Эквивалентно

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

Содержание

Параметры

s - a sub_match to swap with
Type requirements
-
BidirIt must meet the requirements of 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 & ) добавлен