bool empty() const; // (1) C++11
[[nodiscard]] bool empty() const; // (1) C++20
bool empty() const; // (1) C++26
概要
*this が空か否か(つまり、マッチが失敗したか否か)を返す。
戻り値
size() == 0
備考
regex_match、および、regex_searchの引数に渡したmatch_resultsオブジェクトは、マッチが成功するとempty() != trueとなる。
match_resultsオブジェクトの結果が利用可能か否かを確認する場合にはreadyを使用すると良い。regex_iteratorを間接参照して得られるmatch_resultsオブジェクトは、必ずempty() != trueとなる。empty() == trueの場合、match_resultsのメンバ関数には結果が未規定であるものがあるので、注意が必要である(regex_match、および、regex_searchを参照)。- 本メンバ関数は
ready() == falseでも呼び出すことが可能である(その場合、trueが返される)。
例
#include <iostream>
#include <regex>
int main()
{
const char s1[] = " abc ";
const char s2[] = " 012 ";
const std::regex re("\\d+");
std::cmatch m;
// regex_search 実行前
std::cout << std::boolalpha << "ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
// regex_search 実行後(マッチ失敗)
if (std::regex_search(s1, m, re)) {
std::cout << "match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
} else {
std::cout << "not match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
}
// regex_search 実行後(マッチ成功)
if (std::regex_search(s2, m, re)) {
std::cout << "match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
} else {
std::cout << "not match:ready = " << m.ready() << ", empty = " << m.empty() << std::endl;
}
}
出力
ready = false, empty = true
not match:ready = true, empty = true
match:ready = true, empty = false
バージョン
言語
- 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++: ??
参照
- P0600R1
[[nodiscard]]in the Library- C++20で
[[nodiscard]]が付加された
- C++20で
- P2422R1 Remove
nodiscardannotations from the standard library specification- C++26で
[[nodiscard]]指定が削除された
- C++26で