Skip to content

Latest commit

 

History

History
158 lines (123 loc) · 5.84 KB

op_constructor.md

File metadata and controls

158 lines (123 loc) · 5.84 KB

コンストラクタ

  • regex[meta header]
  • std[meta namespace]
  • match_results[meta class]
  • function[meta id-type]
  • cpp11[meta cpp]
match_results(const Allocator& a = Allocator());    // (1)
match_results() : match_results(Allocator()) {}     // (1) C++20

match_results(const Allocator& a);                  // (2) C++20

match_results(const match_results& m);              // (3)

match_results(match_results&& m) noexcept;          // (4)

概要

match_results オブジェクトを構築する。

要件

  • (4) Allocator のムーブコンストラクタは例外で終了しないこと。

効果

  • (1) デフォルトコンストラクタ。
    • C++17まで : 指定したアロケータaを用いてmatch_results オブジェクトを構築する。
    • C++20 : アロケータをデフォルト構築してmatch_results オブジェクトを構築する。
  • (2) 指定したアロケータaを用いてmatch_results オブジェクトを構築する。
  • (3) コピーコンストラクタ。引数 m をコピーした match_results オブジェクトを構築する。
  • (4) ムーブコンストラクタ。引数 m をムーブした match_results オブジェクトを構築する。

事後条件

計算量

  • (1)(2) 定数時間
  • (3) 線形時間
  • (4) 定数時間

備考

規格では明確ではないものの、(3) の形式でも以下の事後条件を満たすべきであると思われる。

#include <iostream>
#include <regex>

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;                   // (1) の形式
  print(m1);

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

  std::cmatch m2(m1);               // (3) の形式
  print(m2);

  std::cmatch m3(std::move(m1));    // (4) の形式
  print(m3);
}
  • m.size()[link size.md]
  • std::regex[link ../basic_regex.md]
  • m.prefix()[link prefix.md]
  • m.suffix()[link suffix.md]
  • m.str[link str.md]
  • m.ready()[link ready.md]
  • std::cmatch[link ../match_results.md]
  • std::regex_search[link ../regex_search.md]
  • std::move[link ../../utility/move.md]

出力

ready:false

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 [mark verified], 3.1 [mark verified], 3.2 [mark verified], 3.3 [mark verified], 3.4 [mark verified], 3.5 [mark verified], 3.6 [mark verified]
  • GCC: 4.9.0 [mark verified], 4.9.1 [mark verified], 5.0.0 [mark verified]
  • ICC: ??
  • Visual C++: ??

備考

GCC(libstdc++) の 4.9.2 までは、regex_iterator を間接参照した結果から (2)、あるいは、(3) の形式で構築した場合に position の結果が正しくコピーされない。これは、4.9.3 以降で修正される予定である。

参照