IsStreamable.hpp
1 // Distributed under the MIT License.
2 // See LICENSE.txt for details.
3 
4 #pragma once
5 
6 #include <type_traits>
7 
8 #include "Utilities/Requires.hpp"
10 #include "Utilities/TypeTraits.hpp"
11 
12 namespace tt {
13 // @{
14 /// \ingroup TypeTraitsGroup
15 /// \brief Check if type `T` has operator<<(`S`, `T`) defined.
16 ///
17 /// \details
18 /// Inherits from std::true_type if the type `T` has operator<<(`S`, `T`)
19 /// defined for a stream `S`, otherwise inherits from std::false_type
20 ///
21 /// \usage
22 /// For any type `T` and stream type `S`,
23 /// \code
24 /// using result = tt::is_streamable<S, T>;
25 /// \endcode
26 ///
27 /// \metareturns
28 /// cpp17::bool_constant
29 ///
30 /// \semantics
31 /// If the type `T` has operator<<(`S`, `T`) defined for stream `S`, then
32 /// \code
33 /// typename result::type = std::true_type;
34 /// \endcode
35 /// otherwise
36 /// \code
37 /// typename result::type = std::false_type;
38 /// \endcode
39 ///
40 /// \example
41 /// \snippet Test_IsStreamable.cpp is_streamable_example
42 /// \see std::cout std::ifstream std::sstream std::ostream
43 /// \tparam S the stream type, e.g. std::stringstream or std::ostream
44 /// \tparam T the type we want to know if it has operator<<
45 template <typename S, typename T, typename = cpp17::void_t<>>
46 struct is_streamable : std::false_type {};
47 
48 /// \cond
49 template <typename S, typename T>
50 struct is_streamable<
51  S, T,
52  cpp17::void_t<decltype(std::declval<std::add_lvalue_reference_t<S>>()
53  << std::declval<T>()),
54  Requires<not std::is_same<S, T>::value>>> : std::true_type {};
55 /// \endcond
56 
57 /// \see is_streamable
58 template <typename S, typename T>
59 constexpr bool is_streamable_v = is_streamable<S, T>::value;
60 
61 /// \see is_streamable
62 template <typename S, typename T>
63 using is_streamable_t = typename is_streamable<S, T>::type;
64 // @}
65 } // namespace tt
void void_t
Given a set of types, returns void
Definition: TypeTraits.hpp:187
Declares stream operators for STL containers.
Defines the type alias Requires.
A collection of useful type traits.
Definition: TensorExpression.hpp:113
C++ STL code present in C++17.
Definition: Array.hpp:16