Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Reflect on the number of fields of a too-complex type at compile time? #451

Closed

Description

@bryceschober

Background:

I'm implementing an rfl::Reflector for a type that has some member types that reflect-cpp isn't capable of reflecting. (FWIW, these are types from https://github.com/mpusz/mp-units, which I assume probably need a similar deep surgery treatment as your std::chrono::duration support.)

Issue:

As a workaround for the time being, I'm implementing rfl::Reflector for these types to convert to-from primitive underlying types. Since these require manual conversion of each field, I'd love to have a standard compile-time assertion when the number of fields in ReflType doesn't match the full type, as in:

namespace rfl{
    template<>
    struct Reflector<FancyType> {
        struct ReflType {
            float field_1;
            uint64_t field_2;
        };
        static_assert(rfl::fields<ReflType>().size() == rfl::fields<FancyType>().size(),
                      "ReflType must have the same number of fields as actual type");

        // Conversion functions go here..

Is there a different constexpr method that reflect-cpp can provide to implement this compile-time assertion? Obviously, we can implement run-time assertions in our unit tests, but that's less than ideal.

Even more ideally, a rfl::Reflector could do something approximately like this, and have reflect-cpp provide the assertion:

namespace rfl{
    template<>
    struct Reflector<FancyType, SameNumFieldsPolicy> {

Activity

  1. bryceschober commented on Jun 17, 2025

    @bryceschober
    ContributorAuthor

    Ah, now I see that I can use rfl::internal::num_fields<T> for that purpose... but obviously that's not "public". Am I missing a different way to do this?

    namespace rfl{
        template<>
        struct Reflector<FancyType> {
            struct ReflType {
                float field_1;
                uint64_t field_2;
            };
            static_assert(rfl::internal::num_fields<ReflType> == rfl::internal::num_fields<FancyType>,
                          "ReflType must have the same number of fields as the actual type");
    
            // Conversion functions go here..
  2. liuzicheng1987 commented on Jun 17, 2025

    @liuzicheng1987
    Collaborator

    @bryceschober I think we can make that public, that's not a problem

  3. bryceschober commented on Jun 18, 2025

    @bryceschober
    ContributorAuthor

    Are there other compile-time reflection capabilities that are similarly internal, like field name and type iteration?

  4. liuzicheng1987 commented on Jun 19, 2025

    @liuzicheng1987
    Collaborator

    @bryceschober field names and types are public

  5. changed the title [-]Reflect on the number of fields at compile time?[/-] [+]Reflect on the number of fields of a too-complex type at compile time?[/+] on Jul 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions