std:: compare_weak_order_fallback
|
ヘッダーで定義
<compare>
|
||
|
inline
namespace
/* unspecified */
{
inline
constexpr
/* unspecified */
|
(C++20以降) | |
|
呼び出しシグネチャ
|
||
|
template
<
class
T,
class
U
>
requires
/* see below */
|
(C++20以降) | |
部分式
t
と
u
に対して三項比較を実行し、
std::weak_ordering
型の結果を生成します。演算子
<=>
が利用できない場合でも同様です。
std:: decay_t < T > と std:: decay_t < U > が同じ型である場合、 std :: compare_weak_order_fallback ( t, u ) は 式等価 です:
- std:: weak_order ( t, u ) 、これが適切な式である場合。そうでない場合、
-
t
==
u
?
std
::
weak_ordering
::
equivalent
:
t < u ? std :: weak_ordering :: less :
std :: weak_ordering :: greater 、ただし式 t == u および t < u が両方とも適切な式であり、 decltype ( t == u ) および decltype ( t < u ) のそれぞれが boolean-testable をモデル化する場合。ただし、 t および u は一度だけ評価される。
それ以外のすべての場合において、 std :: compare_weak_order_fallback ( t, u ) は不適格となり、これはテンプレートのインスタンス化の直接の文脈で現れる場合に substitution failure を引き起こす可能性があります。
目次 |
カスタマイゼーションポイントオブジェクト
名前
std::compare_weak_order_fallback
は
カスタマイゼーションポイントオブジェクト
を表し、これは
関数オブジェクト
のconstな
リテラル
semiregular
クラス型である。詳細は
CustomizationPointObject
を参照のこと。
例
#include <compare> #include <iostream> // <=> をサポートしない struct Rational_1 { int num; int den; // > 0 }; inline constexpr bool operator<(Rational_1 lhs, Rational_1 rhs) { return lhs.num * rhs.den < rhs.num * lhs.den; } inline constexpr bool operator==(Rational_1 lhs, Rational_1 rhs) { return lhs.num * rhs.den == rhs.num * lhs.den; } // <=> をサポートする struct Rational_2 { int num; int den; // > 0 }; inline constexpr std::weak_ordering operator<=>(Rational_2 lhs, Rational_2 rhs) { return lhs.num * rhs.den <=> rhs.num * lhs.den; } inline constexpr bool operator==(Rational_2 lhs, Rational_2 rhs) { return lhs <=> rhs == 0; } void print(int id, std::weak_ordering value) { std::cout << id << ") "; if (value == 0) std::cout << "equal\n"; else if (value < 0) std::cout << "less\n"; else std::cout << "greater\n"; } int main() { Rational_1 a{1, 2}, b{3, 4}; // print(0, a <=> b); // 動作しない print(1, std::compare_weak_order_fallback(a, b)); // 動作する、< と == にフォールバック Rational_2 c{6, 5}, d{8, 7}; print(2, c <=> d); // 動作する print(3, std::compare_weak_order_fallback(c, d)); // 動作する Rational_2 e{2, 3}, f{4, 6}; print(4, e <=> f); // 動作する print(5, std::compare_weak_order_fallback(e, f)); // 動作する }
出力:
1) less 2) greater 3) greater 4) equal 5) equal
不具合報告
以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。
| DR | 適用対象 | 公開時の動作 | 正しい動作 |
|---|---|---|---|
|
LWG 2114
( P2167R3 ) |
C++20 |
フォールバック機構は戻り値の型が
bool に変換可能であることのみを要求 |
制約が強化された |
関連項目
|
(C++20)
|
3方向比較を実行し、型
std::weak_ordering
の結果を生成する
(カスタマイゼーションポイントオブジェクト) |