Antony Polukhin <antoshkka <at> gmail.com> writes:
Hello,
I've come up with a trick that may be useful in Boost.Hana, Boost.Serialization or some other libraries.
Definitely an interesting and novel trick. Although, as you said, it is a nasty trick, and such things would be better if supported in the language (IMO). Until then, this trick might be useful.
[...] * there's no known to me way to make get<>() method constexpr
If we computed the offset at which the n-th member lies inside the struct, couldn't we use something like template <std::size_t I, class T> decltype(auto) get(const T& val) noexcept { std::size_t offsets[] = {}; // assume this is known using U = std::tuple_element_t<I, decltype(detail::as_tuple<T>())>; return *static_cast<U const*>( // <-- is this UB? static_cast<char const*>(std::addressof(val)) + offsets[I] ); } Because if that works, computing the offsets is feasible.
* does not detect constness (because of that get<>() always returns const reference)
Is there any interest in this feature?
That might be interesting for Hana, while we wait for a language feature to make this easier. Louis