Namespaces
Variants

std::less

ja.cppreference.net より
 
 
ユーティリティライブラリ
言語サポート
型サポート (基本型、RTTI)
ライブラリ機能テストマクロ (C++20)
プログラムユーティリティ
可変長引数関数
コルーチンサポート (C++20)
契約サポート (C++26)
三方比較
(C++20)
(C++20)(C++20)(C++20)    
(C++20)(C++20)(C++20)

汎用ユーティリティ
関係演算子 (C++20で非推奨)
 
関数オブジェクト
関数呼び出し
(C++17)(C++23)
恒等関数オブジェクト
(C++20)
透過的な演算子ラッパー
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

旧式のバインダとアダプタ
(C++17まで*)
(C++17まで*)
(C++17まで*)
(C++17まで*)  
(C++17まで*)
(C++17まで*)(C++17まで*)(C++17まで*)(C++17まで*)
(C++20まで*)
(C++20まで*)
(C++17まで*)(C++17まで*)
(C++17まで*)(C++17まで*)

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

比較を行うための関数オブジェクト。プライマリテンプレートは型 operator< に対して T を呼び出します。

特殊化

関数オブジェクト、パラメータと戻り値の型を推論して 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
(クラス)