Skip to content

Commit 82b302e

Browse files
authored
Make half stream operator friend method (#6277)
This PR moves the half stream operators from outside of the half class to a friend member function. The change is necessary as otherwise the operators are not available if the user calls them outside of the global namespace. There is no change in function behaviour.
1 parent 9a31975 commit 82b302e

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

sycl/include/CL/sycl/half_type.hpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,20 @@ class half {
585585
return static_cast<float>(Data);
586586
}
587587

588+
// Operator << and >>
589+
inline friend std::ostream &operator<<(std::ostream &O,
590+
cl::sycl::half const &rhs) {
591+
O << static_cast<float>(rhs);
592+
return O;
593+
}
594+
595+
inline friend std::istream &operator>>(std::istream &I, cl::sycl::half &rhs) {
596+
float ValFloat = 0.0f;
597+
I >> ValFloat;
598+
rhs = ValFloat;
599+
return I;
600+
}
601+
588602
template <typename Key> friend struct std::hash;
589603

590604
friend class sycl::ext::intel::esimd::detail::WrapperElementTypeProxy;
@@ -694,17 +708,5 @@ template <> struct numeric_limits<cl::sycl::half> {
694708

695709
} // namespace std
696710

697-
inline std::ostream &operator<<(std::ostream &O, cl::sycl::half const &rhs) {
698-
O << static_cast<float>(rhs);
699-
return O;
700-
}
701-
702-
inline std::istream &operator>>(std::istream &I, cl::sycl::half &rhs) {
703-
float ValFloat = 0.0f;
704-
I >> ValFloat;
705-
rhs = ValFloat;
706-
return I;
707-
}
708-
709711
#undef __SYCL_CONSTEXPR_HALF
710712
#undef _CPP14_CONSTEXPR

sycl/test/type_traits/half_operator_types.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ void check_half_logical_operator_types(sycl::queue &Queue) {
8282
});
8383
}
8484

85+
template <typename T1>
86+
void check_half_stream_operator_type(sycl::queue &Queue) {
87+
88+
// Host only stream test
89+
std::istringstream iss;
90+
std::ostringstream oss;
91+
sycl::half val;
92+
static_assert(is_same_v<decltype(iss >> val), std::istream &>);
93+
static_assert(is_same_v<decltype(oss << val), std::ostream &>);
94+
}
95+
8596
int main() {
8697

8798
sycl::queue Queue;

0 commit comments

Comments
 (0)