Simplify template has_field with C++17/20
If C++20 is on the table, you can do that with a concept that checks a simple requirement
template <typename T>
concept has_value = requires(T) {
T::value;
};
template<typename T> requires has_value<T>
std::ostream& operator<<(std::ostream& os, T const& arg)
{
return os << arg.value;
}
T::value
being a well formed expression is being checked in the requires expression. Pretty straight forward to write, and to use as a constraint on a template.
In c++17
template<typename,typename=void> constexpr bool has_value = false;
template<typename T> constexpr bool has_value<T,decltype(T::value,void())> = true;
Test
struct V { int value; };
struct W { int walue; };
static_assert(has_value<V>);
static_assert(not has_value<W>);
Thanks to https://stackoverflow.com/a/52291518/3743145