Add static_assert for better error messages
sharkdp opened this issue · comments
Obviously, we can not create a painless::Parameter<T>
for any T
. We should think about the proper constraint and add a static_assert
such that users will see a better error message.
For now, we probably require existence of ostream& operator<<
and istream& operator>>
for "serialization" and "deserialization".
Just reuse the is_detected
bits that I wrote for dbg
.
https://github.com/sharkdp/dbg-macro/blob/4c59a8662161f4b3d78361cc1e677968a80fc170/dbg.h#L252-L286
The ostream
detection is already there.
https://github.com/sharkdp/dbg-macro/blob/4c59a8662161f4b3d78361cc1e677968a80fc170/dbg.h#L327-L332
And istream
detection is just straight-forward.
template <typename T>
using istream_operator_t =
decltype(std::declval<std::istream&>() >> std::declval<T>());
template <typename T>
struct has_istream_operator : is_detected<istream_operator_t, T> {};
Then you can just do
static_assert(detail::has_ostream_operator<const T&>::value,
"Type does not support the << ostream operator");
but you probably want to use the tag dispatch that I used in dbg
https://github.com/sharkdp/dbg-macro/blob/4c59a8662161f4b3d78361cc1e677968a80fc170/dbg.h#L345-L366
Just reuse the
is_detected
bits that I wrote fordbg
.
https://github.com/sharkdp/dbg-macro/blob/4c59a8662161f4b3d78361cc1e677968a80fc170/dbg.h#L252-L286
Exactly what I was thinking about when creating this ticket 👍