-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access tuple member by type: hana::at(hana::type_c<T>, xs)
#285
Comments
I'd suggest The map solution is my favourite. If you need a tuple you can always create a map to the indices. Or perhaps something like a function called #include <boost/hana.hpp>
#include <utility>
namespace hana = boost::hana;
struct A { int value; };
struct B { int value; };
struct C { int value; };
struct D { int value; };
template <typename T, typename Searchable>
auto get(Searchable&& xs)
{
return *hana::find_if(std::forward<Searchable>(xs), [](auto const& x)
{
return hana::equal(hana::decltype_(x), hana::type_c<const T>);
});
}
int main()
{
auto my_tuple = hana::make_tuple(
A{1},
B{2},
C{3},
D{4}
);
BOOST_HANA_RUNTIME_ASSERT(get<A>(my_tuple).value == 1);
BOOST_HANA_RUNTIME_ASSERT(get<B>(my_tuple).value == 2);
BOOST_HANA_RUNTIME_ASSERT(get<C>(my_tuple).value == 3);
BOOST_HANA_RUNTIME_ASSERT(get<D>(my_tuple).value == 4);
} A real implementation would obviously be a bit messier to be more performant and support returning references. |
Not supporting The solution would really be to allow returning references from We could also consider providing |
I know I'm digging on some old stuff, but perhaps I don't know how bad of a breaking change that would be for some algorithms like Just an idea 💡 err maybe this would be a case for |
Sometimes I have an
hana::tuple<A, B, C, D>
and I want to access members by type, likestd::get<T>
for tuples. Ideally, I would like to sayhana::at(hana:.type_c<A>, some_tuple)
. I currently found two workarounds:A
inside the tuple, then access by index.{type_c<A> -> A; type_c<B> -> B; ...}
map from the tuple and use that instead.Is not having a
hana::at(hana::type_c<T>, xs)
a design choice? If not, I think it would be a beneficial feature for accessing tuples more easily and supporting more familiarstd::tuple
operations.The text was updated successfully, but these errors were encountered: