boostorg / boostorg/hana

Can't retrieve member of model of Struct by reference

Open
#384 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
1.9k
Forks
225
PR merge metrics
No merged PRs in 30d

Description

For following reflectable Struct

struct triangles {
BOOST_HANA_DEFINE_STRUCT(triangles,
(vec, vertices),
(vec, indices)
);
};

I would like to obtain references to `vertices` and `indices`, but am unable to do using `hana::members` or `hana::accessors`. I am, on the other hand, able to obtain copies of said elements. Here's an example of the code that attempts to extract a const reference to the indices:

auto maybe_indices_ix = hana::index_if(
hana::accessors(),
[] (auto field) {
auto t = hana::type_c<
typename remove_pointer<
typename decay<
decltype(hana::second(field)(declval()))
>::type
>::type
>;
return render::traits::is_vector_of_indices(t);
}
);
constexpr auto indices_ix = hana::value(*maybe_indices_ix);
constexpr auto accessors = hana::accessors();
constexpr auto indices_accessor = accessors[hana::int_c];
constexpr auto indices_of = hana::second(indices_accessor);
const auto& indices = indices_of(triangles);

If I replace the last line with `auto indices = ...` then it works. The same result is obtained if I use `hana::members` as per manual.

Am I doing something wrong? Assuming I'm not, I hope there's a workaround, because copying vectors of vertices and indices would incur an unacceptable performance penalty in such a tight rendering loop.

Thanks for the awesome library by the way!

Contributor guide

Open the contributing guide

Research direction

Start by reducing the example around hana::members, hana::accessors, and the accessor call on triangles, comparing the const-reference and copy forms. Confirm whether the library can return references to vertices and indices without copying; done means a documented workaround or corrected behavior that preserves references in this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.