Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions stl/inc/thread
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <tuple>
#include <xthreads.h>
#if _HAS_CXX20
#include <compare>
#include <stop_token>
#endif // _HAS_CXX20

Expand Down Expand Up @@ -215,7 +216,11 @@ private:
friend thread::id thread::get_id() const noexcept;
friend thread::id this_thread::get_id() noexcept;
friend bool operator==(thread::id _Left, thread::id _Right) noexcept;
#if _HAS_CXX20
friend strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept;
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
friend bool operator<(thread::id _Left, thread::id _Right) noexcept;
#endif // !_HAS_CXX20
Comment on lines 218 to +223
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to default the operator== for C++20 and make them hidden friends?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can't be hidden friends; the difference is observable. Arguably operator== also cannot be defaulted because doing so would make it constexpr and we're forbidden to make things constexpr where the Standard doesn't depict them as such.

template <class _Ch, class _Tr>
friend basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id);
friend hash<thread::id>;
Expand All @@ -237,6 +242,11 @@ _NODISCARD inline bool operator==(thread::id _Left, thread::id _Right) noexcept
return _Left._Id == _Right._Id;
}

#if _HAS_CXX20
_NODISCARD inline strong_ordering operator<=>(thread::id _Left, thread::id _Right) noexcept {
return _Left._Id <=> _Right._Id;
Copy link
Contributor

@CaseyCarter CaseyCarter Jan 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fun fact: the Standard doesn't require the total ordering over thread ids that this function observes to be consistent with operator==, so we could have the two functions order them differently ;)

(I've submitted an LWG issue to fix this.)

}
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
_NODISCARD inline bool operator!=(thread::id _Left, thread::id _Right) noexcept {
return !(_Left == _Right);
}
Expand All @@ -256,6 +266,7 @@ _NODISCARD inline bool operator>(thread::id _Left, thread::id _Right) noexcept {
_NODISCARD inline bool operator>=(thread::id _Left, thread::id _Right) noexcept {
return !(_Left < _Right);
}
#endif // !_HAS_CXX20

template <class _Ch, class _Tr>
basic_ostream<_Ch, _Tr>& operator<<(basic_ostream<_Ch, _Tr>& _Str, thread::id _Id) {
Expand Down
9 changes: 9 additions & 0 deletions tests/std/tests/P1614R2_spaceship/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stack>
#include <string>
#include <system_error>
#include <thread>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
Expand Down Expand Up @@ -481,6 +482,14 @@ void ordering_test_cases() {

spaceship_test<std::strong_ordering>(c_mem[0], c_mem[0], c_mem[1]);
}
{ // thread::id
std::thread::id id1;
std::thread::id id1_equal;
std::thread::id id2 = std::this_thread::get_id();

// Implementation-specific assumption: std::thread::id{} occurs first in the unspecified total ordering.
spaceship_test<std::strong_ordering>(id1, id1_equal, id2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there's an implementation assumption here - nothing in the Standard appears to require that std::thread::id{} occurs first in the unspecified total ordering. (It's a safe assumption for us, since we use 0 and store unsigned int.) I'll push a comment.

}
}

template <class Element, class Ordering>
Expand Down