xtd 0.2.0
Loading...
Searching...
No Matches
xtd::icomparable< type_t > Class Template Referenceabstract
Inheritance diagram for xtd::icomparable< type_t >:
xtd::interface xtd::comparison_operators< type_t, icomparable< type_t > > xtd::enum_object< enum_t >

Definition

template<typename type_t>
class xtd::icomparable< type_t >

Defines a generalized comparison method that a value type or class implements to create a type-specific comparison method.

Namespace
xtd
Library
xtd.core
Examples
The following example shows how to use xtd::icomparable interface.
#include <xtd/console>
#include <xtd/icomparable>
using namespace xtd;
class foo : public icomparable<foo> {
public:
explicit foo(int value) : value_ {value} {}
int compare_to(const foo& value) const noexcept override {
if (value_ < value.value_) return -1;
if (value_ > value.value_) return 1;
return 0;
}
private:
int value_ = 0;
};
auto main() -> int {
console::write_line("foo {{42}}.compare_to(foo {{42}}) = {}", foo {42}.compare_to(foo {42}));
console::write_line("foo {{42}}.compare_to(foo {{41}}) = {}", foo {42}.compare_to(foo {41}));
console::write_line("foo {{42}}.compare_to(foo {{43}}) = {}", foo {42}.compare_to(foo {43}));
console::write_line("foo {{42}} <=> foo {{42}} = {}", foo {42} <=> foo {42});
console::write_line("foo {{42}} <=> foo {{41}} = {}", foo {42} <=> foo {41});
console::write_line("foo {{42}} <=> foo {{43}} = {}", foo {42} <=> foo {43});
console::write_line("foo {{42}} <= foo {{42}} = {}", foo {42} <= foo {42});
console::write_line("foo {{42}} <= foo {{41}} = {}", foo {42} <= foo {41});
console::write_line("foo {{42}} <= foo {{43}} = {}", foo {42} <= foo {43});
console::write_line("foo {{42}} < foo {{42}} = {}", foo {42} < foo {42});
console::write_line("foo {{42}} < foo {{41}} = {}", foo {42} < foo {41});
console::write_line("foo {{42}} < foo {{43}} = {}", foo {42} < foo {43});
console::write_line("foo {{42}} >= foo {{42}} = {}", foo {42} >= foo {42});
console::write_line("foo {{42}} >= foo {{41}} = {}", foo {42} >= foo {41});
console::write_line("foo {{42}} >= foo {{43}} = {}", foo {42} >= foo {43});
console::write_line("foo {{42}} > foo {{42}} = {}", foo {42} > foo {42});
console::write_line("foo {{42}} > foo {{41}} = {}", foo {42} > foo {41});
console::write_line("foo {{42}} > foo {{43}} = {}", foo {42} > foo {43});
}
// This code produces the following output :
//
// ffoo {42}.compare_to(foo {42}) = 0
// foo {42}.compare_to(foo {41}) = 1
// foo {42}.compare_to(foo {43}) = -1
//
// foo {42} <=> foo {42} = equivalent
// foo {42} <=> foo {41} = greater
// foo {42} <=> foo {43} = less
//
// foo {42} <= foo {42} = true
// foo {42} <= foo {41} = false
// foo {42} <= foo {43} = true
//
// foo {42} < foo {42} = false
// foo {42} < foo {41} = false
// foo {42} < foo {43} = true
//
// foo {42} >= foo {42} = true
// foo {42} >= foo {41} = true
// foo {42} >= foo {43} = false
//
// foo {42} > foo {42} = false
// foo {42} > foo {41} = true
// foo {42} > foo {43} = false
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.h:21
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Examples
generic_ienumerable2.cpp, generic_ienumerator.cpp, icomparable.cpp, ienumerable.cpp, ienumerator.cpp, and sprintf_class.cpp.

Public Methods

virtual int32 compare_to (const type_t &obj) const noexcept=0
 Compares the current instance with another object of the same type.
 

Member Function Documentation

◆ compare_to()

template<typename type_t >
virtual int32 xtd::icomparable< type_t >::compare_to ( const type_t &  obj) const
pure virtualnoexcept

Compares the current instance with another object of the same type.

Parameters
objAn object to compare with this instance.
Returns
A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
Value Condition
Less than zero This instance is less than obj.
Zero This instance is equal to obj.
Greater than zero This instance is greater than obj.

Implemented in xtd::any_object, xtd::box< type_t >, xtd::date_time, xtd::guid, xtd::time_span, xtd::time_zone_info, and xtd::version.


The documentation for this class was generated from the following file: