std::compare_three_way not compiling

Hi,

I have this code:

1
2
3
4
5
6
7
8
9
10
template<typename T>
struct Value
{
	T val{};

	auto operator<=>(const Value& rhs) const noexcept(noexcept(val<=>val))
	{
		return std::compare_three_way{}(val <=> rhs.val);
	}
};


It does not compile!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct BB
{
	int num;

	bool operator==(const BB&) const = default;
	auto operator<=>(const BB& rhs) const noexcept -> std::strong_ordering
	{
		return num <=> rhs.num;
	}
};

void useValue()
{
	Value<BB> a{ 10 }, b{ 14 };
	bool less = a < b;
}


What am I doing wrong?

Thanks!
Are you compiling as C++20 ?
This works:

1
2
3
4
5
auto operator<=>(const Value& rhs) const noexcept(noexcept(val <=> val))
{
	return val <=> rhs.val;
}
yes I am compiling with C++20
the problem is with

 
std::compare_three_way


it says:

 
call to object of class type 'std::compare_three_way': no matching call operator found

Last edited on
JUANDENT wrote:
it says: call to object of class type 'std::compare_three_way': no matching call operator found


Operator expects arguments, not comparison, therefore:

return std::compare_three_way{}(val, rhs.val);
Last edited on
thank you!!
cppreference has excellent examples on how to use C++ stdlib functionality.

https://en.cppreference.com/w/cpp/utility/compare/compare_three_way

Admittedly the site isn't designed to learn C++ from 'cold', a bit of determination and prior knowledge is required for best results.
Registered users can post here. Sign in or register to post.