C++ Logo

sg7

Advanced search

[SG7] string. vector vs string_view, span in reflection API

From: Matus Chochlik <chochlik_at_[hidden]>
Date: Sat, 10 Apr 2021 03:09:22 +0200
On Sat, Apr 10, 2021 at 2:20 AM David Rector <davrec_at_[hidden]> wrote:

>
>
> On Apr 9, 2021, at 8:35 AM, Matus Chochlik via SG7 <sg7_at_[hidden]>
> wrote:
> This still happens at compile-time inside of the compiler, so it could
> allocate the filtered range of metaobjects by using some internal
> mechanism, that is not exposing generic "dynamic" allocation in consteval.
> Just saying, in case solving dynamic allocation in constexpr/consteval
> contexts is something blocking us moving forward with reflection.
> Or am I missing something?
>
>
>
> To go further, why should the API include something that requires these
> allocations in the first place, since they don’t seem particularly
> efficient (or at least won’t scale well) even if the compiler can handle
> them internally?
>
> That is, should we really be encouraging users to use things like
> `meta::members_of(reflection, predicate)`, which always necessitates a new
> allocation?
>
> It would be more efficient, and permit more natural programming, to only
> expose `meta::members_of(reflection)` (i.e. no predicate argument) and
> instead encourage the user to condition actions on those members via
> control flow statements during iteration over the compiler’s
> already-allocated container of members.
>
> This would require expanding the contexts in which `consteval {}`
> metaprograms are permitted, and introducing corresponding fragment kinds to
> permit injection of entities in those contexts.
>
> That is the precisely the argument of this paper:
> https://isocpp.org/files/papers/P2353R0.html, a cleaned up version of the
> paper “The syntactic gap” I shared on here awhile ago, which should appear
> in the next mailing.
>
> Matus here is how I would write your `unpack_range`, using P2353 features:
>
> ```
> template <std::experimental::meta::info MO>
> consteval auto unpack_range() {
> return unpacked_range<
> consteval {
> template for (auto mem : meta::members_of(MO))
> if (meta::is_constructor(mem))
> << ^<frag::targ>{ mem }; //a template argument fragment
> }
> >{};
> ```
>
> This requires no allocations and is simple to read and write.
>
> Something to consider moving forward, given the emphasis on efficiency.
>

I'm just guessing about Andrew and Wyatt's or Daveed and Faisal's
motivations here, but based on my reflection implementation experience I'd
say the reason why we want to do some of the filtering inside of the
compiler might be that "materializing" the whole:

meta::members_of(MO)

that can then be filtered with arbitrary consteval C++ code may be quite
"heavy" for some meta-objects. for example if:

auto MO = ^std;
//or
auto MO = ^boost;

and I do think that reflecting namespaces and getting (some of) their
members has valid and useful use-cases.
When I did the POC implementation for what became the TMP-based reflection
TS I had metaobject-sequences (or ranges) be meta-objects themselves and
instantiated the elements lazily. It worked with classes, etc. but I don't
have much data for reflected namespaces.

--Matus

Received on 2021-04-09 20:09:35