Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::compar...er_fallback(3)  C++ Standard Libary std::compar...er_fallback(3)

NAME
       std::compare_weak_order_fallback	- std::compare_weak_order_fallback

Synopsis
	  Defined in header <compare>
	  inline namespace /* unspecified */ {

	      inline	     constexpr	       /*	 unspecified	    */
       (since C++20)
		  compare_weak_order_fallback =	/* unspecified */;

	  }
	  Call signature
	  template< class T, class U >

	      requires /* see below */
	  constexpr					    std::weak_ordering
       (since C++20)

	      compare_weak_order_fallback(  T&&	t, U&& u ) noexcept(/* see be-
       low
	  */);

	  Performs three-way comparison	on subexpressions t and	u and produces
       a result	of
	  type std::weak_ordering, even	if the operator	<=> is unavailable.

	  If std::decay_t<T> and std::decay_t<U> are the same type,
	  std::compare_weak_order_fallback(t, u) is expression-equivalent to:

	    * std::weak_order(t, u), if	it is a	well-formed expression;	other-
       wise,
	    * t	== u ? std::weak_ordering::equivalent :
	      t	<  u ? std::weak_ordering::less	:
	      std::weak_ordering::greater, if the expressions t	== u and t < u
       are
	      both well-formed and each	of decltype(t == u) and	 decltype(t  <
       u) models
	      boolean-testable,	except that t and u are	evaluated only once.

	  In  all  other cases,	std::compare_weak_order_fallback(t, u) is ill-
       formed, which can
	  result in substitution failure when it appears in the	immediate con-
       text of a
	  template instantiation.

Example
       // Run this code

	#include <compare>
	#include <iostream>

	// does	not support <=>
	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;
	}

	// supports <=>
	struct Rational_2
	{
	    int	num;
	    int	den; //	> 0
	};

	inline constexpr std::weak_ordering operator<=>(Rational_2 lhs,	Ratio-
       nal_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); // does not work
	    print(1, std::compare_weak_order_fallback(a, b));  //  works,  de-
       faults to < and ==

	    Rational_2 c{6, 5},	d{8, 7};
	    print(2, c <=> d); // works
	    print(3, std::compare_weak_order_fallback(c, d)); // works

	    Rational_2 e{2, 3},	f{4, 6};
	    print(4, e <=> f); // works
	    print(5, std::compare_weak_order_fallback(e, f)); // works
	}

Output:
	1) less
	2) greater
	3) greater
	4) equal
	5) equal

	  Defect reports

	  The following	behavior-changing defect reports were applied retroac-
       tively to
	  previously published C++ standards.

	     DR	    Applied to	       Behavior	as published		  Cor-
       rect behavior
	  LWG  2114   C++20	  the  fallback	mechanism only required	  con-
       straints	strengthened
	  (P2167R3)	       return types to be convertible to bool

See also
	  weak_order performs 3-way comparison and produces a result of	type
	  (C++20)    std::weak_ordering
		     (customization point object)

http://cppreference.com		  2024.06.10	  std::compar...er_fallback(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::compare_weak_order_fallback&sektion=3&manpath=FreeBSD+Ports+15.1.quarterly>

home | help