Skip to content

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

@bryceschober

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> {

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions