Recently I have come across the question discussed is_streamable
type trait. So I decided to implement my own version and come up with the next solution to check if the type can be read from std::istream
or not:
template<typename, typename = void>
struct is_readable_from_stream_impl
: std::false_type {};
template<typename T>
struct is_readable_from_stream_impl<T,
std::void_t<decltype(std::declval<std::istream&>() >> std::declval<T>())>>
: std::true_type {};
template<typename T>
struct is_readable_from_stream :
is_readable_from_stream_impl<T> {};
template<typename T>
inline constexpr auto is_readable_from_stream_v = is_readable_from_stream<T>::value;
So far, so good. I added then a custom structure with overloaded operator>>
:
struct readable {};
std::istream& operator>>(std::istream& is, readable&)
{
return is;
}
and tested the type trait:
static_assert(is_readable_from_stream_v<readable&>);
static_assert(!is_readable_from_stream_v<readable>);
The checks were passed with both gcc 8.2
and clang 6.0.0
, but MSVC
rejects the second assert.
I am wondering if I my implementation (or test) is incorrect or it is another issue with MSVC
.