Example
#include <tuple>
struct foo {
int i{};
bool b{};
float f{};
};
constexpr auto f = foo{.i = 42, .b = true, .f = 4.2f};
static_assert(42 == std::get<0>(f) and std::get<1>(f) and 4.2f == std::get<2>(f));
Puzzle
- Can you extend std::get to support aggregates?
#include <tuple>
struct foo {
int i{};
bool b{};
float f{};
};
constexpr auto f = foo{.i = 42, .b = true, .f = 4.2};
static_assert(42 == std::get<0>(f) and std::get<1>(f) and 4.2 == std::get<2>(f));
Solutions
template <typename T, std::size_t I, typename = void>
struct has_get : std::false_type {};
template <typename T, std::size_t I>
struct has_get<T, I, std::void_t<decltype(std::get<I>(std::declval<T>()))>>
: std::true_type {};
namespace std {
template <typename T>
concept DoesNotHaveGetConcept = requires(T t) { !has_get<T, 0>::value; };
namespace mp = boost::mp;
template <std::size_t I>
constexpr auto get(DoesNotHaveGetConcept auto s) {
return std::get<I>(mp::reflection::to_tuple(s));
}
} // namespace std