最終更新日時:
が更新

履歴 編集

function
<regex>

std::match_results::operator=(C++11)

match_results& operator=(const match_results& m);       // (1)

match_results& operator=(match_results&& m) noexcept;   // (2)

概要

match_results オブジェクトを代入する。

要件

  • (1) value_typesub_match<BidirectionalIterator>)はこのコンテナに対してコピー挿入可能(CopyInsertable)であること。
  • (2) allocator_traits<allocator_type>::propagate_on_container_move_assignment::value == false である場合、value_typesub_match<BidirectionalIterator>)はこのコンテナに対してムーブ挿入可能(MoveInsertable)であること。

効果

  • (1) コピー代入演算子。引数 m*this にコピー代入する。
  • (2) ムーブ代入演算子。引数 m*this にムーブ代入する。*this の全ての既存の要素はムーブ代入されるか破棄される。

事後条件

計算量

  • (1) 線形時間
  • (2) 線形時間

#include <iostream>
#include <regex>
#include <utility>

void print(const std::cmatch& m)
{
  std::cout << "ready:" << std::boolalpha << m.ready() << std::endl;
  if (m.ready()) {
    std::cout << "prefix:'" << m.prefix() << '\'' << std::endl;
    for (std::size_t i = 0, n = m.size(); i < n; ++i) {
      std::cout << i << ":'" << m.str(i) << '\'' << std::endl;
    }
    std::cout << "suffix:'" << m.suffix() << '\'' << std::endl;
  }
  std::cout << std::endl;
}

int main()
{
  const char s[] = " abc 123 def ";
  const std::regex re("(\\w+) (\\d+) (\\w+)");

  std::cmatch m1, m2, m3;

  std::regex_search(s, m1, re);
  print(m1);

  m2 = m1;                          // (1) の形式
  print(m2);

  m3 = std::move(m1);               // (2) の形式
  print(m3);
}

出力

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

ready:true
prefix:' '
0:'abc 123 def'
1:'abc'
2:'123'
3:'def'
suffix:' '

バージョン

言語

  • C++11

処理系

  • Clang: 3.0 , 3.1 , 3.2 , 3.3 , 3.4 , 3.5 , 3.6
  • GCC: 4.9.0 , 4.9.1 , 5.0.0
  • ICC: ??
  • Visual C++: ??

備考

GCC(libstdc++) の 4.9.2 までは、アロケータが上記の事後条件のようには設定されず、また、regex_iterator を間接参照した結果を代入した場合に position の結果が正しくコピーされない。これは、4.9.3 以降で修正される予定である。

参照

  • LWG Issue 2184. Muddled allocator requirements for match_results assignments
    • コピー代入・ムーブ代入がサポートされることが明確化された。C++11時点の規格は、クラス概要でコピー代入・ムーブ代入演算子を宣言する一方、要件の除外句が「const修飾されたシーケンスコンテナの操作のみサポート」と読め、両者が矛盾していた。この修正はC++20に取り込まれたが、矛盾した文言を正すものであり、演算子自体はC++11から存在してアロケータ対応コンテナの規則に従うため、実装の挙動は変わらない(欠陥修正のため処理系は早期に対応している)