Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::ranges::less — less-than comparison

std::ranges::less is a function object type from <functional> that compares two arguments. It supports compatible different types, requires std::totally_ordered_with, and returns bool.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <functional>
std::ranges::less{}(a, b)

std::ranges::less is a type, not a template: std::ranges::less{} creates a callable object. Include <functional>. Its templated operator() is constexpr, returns bool, and normally evaluates std::forward<T>(t) < std::forward<U>(u).

Calling it requires std::totally_ordered_with<T, U>. An operator< alone is insufficient: consistent equality and ordering comparisons and a suitable common reference type are required. Different argument types are allowed when they meet the requirements. Syntactic validity does not prove that the semantic ordering laws hold.

The type exposes is_transparent, so it does not fix a single argument type. When comparison uses built-in pointer <, it uses an implementation-defined strict total order; conversions to the pointer type must preserve equality. Do not assume a particular ordering of unrelated objects.

The example compares numbers, including int with long. Equal arguments produce false. The expected output is true, false, false, each on a separate line.

Examples

#include <functional>
#include <iostream>

int main() {
    constexpr std::ranges::less less{};
    static_assert(less(2, 5L));
    static_assert(!less(5, 5));

    std::cout << std::boolalpha
              << less(2, 5L) << '\n'
              << less(5, 5) << '\n'
              << less(8, 3) << '\n';
}

Result

true
false
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources