Namespaces
Variants

std:: less

From cppreference.net
Utilities library
Function objects
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
ヘッダーで定義 <functional>
template < class T >
struct less ;
(C++14まで)
template < class T = void >
struct less ;
(C++14から)

比較を実行するための関数オブジェクト。主要なテンプレートは型 T に対して operator < を呼び出します。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳 - C++関連の専門用語(Specializations、Member types、Member functions、std::less、operator()、Parameters、Return value、Exceptions、Possible implementation、Example、Defect reports、See also)は原文のまま保持 - HTMLタグ、属性、数値、コード関連の要素は一切変更せず - フォーマットと構造を完全に維持

特殊化

関数オブジェクト、パラメータと戻り値の型を推論して x < y を実装
(クラステンプレートの特殊化)

メンバー型

定義
result_type (C++17で非推奨) (C++20で削除) bool
first_argument_type (C++17で非推奨) (C++20で削除) T
second_argument_type (C++17で非推奨) (C++20で削除) T

これらのメンバ型は、公開継承によって std:: binary_function < T, T, bool > から取得されます。

(C++11まで)

メンバー関数

operator()
第1引数が第2引数より 小さい かどうかをチェックする
(public member function)

std::less:: operator()

bool operator ( ) ( const T & lhs, const T & rhs ) const ;
(constexpr since C++14)

lhs rhs より 小さい かどうかをチェックします。

パラメータ

lhs, rhs - 比較する値

戻り値

lhs < rhs

T がポインタ型の場合、結果は 実装定義のポインタに対する厳密な全順序 と一致します。

例外

実装定義の例外をスローする可能性があります。

実装例

constexpr bool operator()(const T& lhs, const T& rhs) const 
{
    return lhs < rhs; // assumes that the implementation handles pointer total order
}

#include <functional>
template<typename A, typename B, typename C = std::less<>>
constexpr bool fun(A a, B b, C cmp = C{})
{
    return cmp(a, b);
}
static_assert(fun(1, 2) == true);
static_assert(fun(1.0, 1) == false);
static_assert(fun(1, 2.0) == true);
static_assert(std::less<int>{}(5, 5.6) == false);   // 5 < 5 (警告: 暗黙的変換)
static_assert(std::less<double>{}(5, 5.6) == true); // 5.0 < 5.6
static_assert(std::less<int>{}(5.6, 5.7) == false); // 5 < 5 (警告: 暗黙的変換)
static_assert(std::less{}(5, 5.6) == true);         // less<void>: 5.0 < 5.6
int main() {}

欠陥報告

以下の動作変更の欠陥報告書は、以前に公開されたC++規格に対して遡及的に適用されました。

DR 適用対象 公開時の動作 正しい動作
LWG 2562 C++98 the pointer total order might be inconsistent guaranteed to be consistent

関連項目

関数オブジェクト: x == y
(クラステンプレート)
関数オブジェクト: x > y
(クラステンプレート)
制約付き関数オブジェクト: x < y
(クラス)