Skip to content

API/Reflection: no public fallible in-place struct builder forces raw byte-buffer placement-new, duplicated rfl::internal::ptr_cast, and manual destructor rollback #188

Not planned

Description

@clockblocker

Part of #138
Popularity: 1/7 corpus consumers (sqlgen)

Related issues

Tangentially touches #143 (exceptions requirement): where sqlgen tries to avoid raw byte-buffer placement-new by using rfl::from_named_tuple in DuckDB, it is forced back into throwing .value()... inside try/catch.

Problem

reflect-cpp exposes rfl::to_view (to inspect/mutate an already-constructed struct) and rfl::from_named_tuple<T> (to convert an already-constructed rfl::NamedTuple into T), while keeping its field-by-field fallible construction & rollback machinery inside rfl::parsing::NamedTupleParser and rfl::internal::ptr_cast.

Custom format or ORM backends that deserialize positional/columnar records into arbitrary user structs T (which may not be default-constructible) without routing through rfl::parsing::Parser<R, W, T> face an unpleasant dilemma:

  1. Either re-implement raw aligned byte storage + ptr_cast + rfl::to_view on uninitialized memory + placement ::new + manual destructor rollback on error and after move; OR
  2. Construct rfl::named_tuple_t<T>(read_field<is>(...).value()...) via throwing .value() inside a try/catch block because there is no Result-propagating pack-to-struct builder.

Observed Downstream Workaround

munich-data-engineering/sqlgen (265b0a17)

sqlgen hits both horns of this dilemma across its backends:

  • Raw byte-buffer + placement-new + manual destructor rollback + copied rfl::internal::ptr_cast:

    • include/sqlgen/internal/from_str_vec.hpp#L38,L72-L86:
      template <class T>
      Result<T> from_str_vec(
          const std::vector<std::optional<std::string>>& _str_vec) {
        alignas(T) unsigned char buf[sizeof(T)]{};
        auto ptr = ptr_cast<T*>(&buf);
        auto view = rfl::to_view(*ptr);
        const auto [err, num_fields_assigned] = read_into_view(_str_vec, &view);
        if (err) [[unlikely]] {
          call_destructors_where_necessary(num_fields_assigned, &view);
          return error(err->what());
        }
        auto res = Result<T>(std::move(*ptr));
        call_destructors_where_necessary(num_fields_assigned, &view);
        return res;
      }
      with placement-new on include/sqlgen/internal/from_str_vec.hpp#L38:
      ::new (rfl::get<i>(*_view)) T(std::move(*res));
    • include/sqlgen/internal/ptr_cast.hpp#L6-L18: Prior to commit d3de060, from_str_vec.hpp called rfl::internal::ptr_cast<T*>(&buf) directly; in d3de060, sqlgen copied rfl::internal::ptr_cast verbatim along with its GCC 12 workaround comment:
      /// Normally, we would use std::launder(reinterpret_cast<...>(...)),
      /// but there are weird issues on GCC 12 under certain compiler settings,
      /// so we are using this workaround instead.
      template <class T1, class T2>
      inline T1 ptr_cast(T2* _ptr) {
        return static_cast<T1>(static_cast<void*>(_ptr));
      }
    • include/sqlgen/internal/call_destructors_where_necessary.hpp#L9-L35: duplicates reflect-cpp's manual per-field destructor loop over ViewType::Fields.
  • Exception-based fallback when using rfl::from_named_tuple (DuckDB backend):

    • include/sqlgen/duckdb/from_chunk_ptrs.hpp#L28-L39:
      try {
        return rfl::from_named_tuple<T>(rfl::named_tuple_t<T>(
            duckdb::parsing::Parser<typename FieldTs::Type>::read(
                rfl::get<_is>(_chunk_ptrs).is_not_null(_i)
                    ? rfl::get<_is>(_chunk_ptrs).data + _i
                    : nullptr)
                .value()...));
      } catch (const std::exception& e) {
        return error(e.what());
      }

Root Cause in reflect-cpp & Suggested Direction

Calling rfl::to_view(*ptr) on an uninitialized unsigned char buf[sizeof(T)] is fragile (UB-adjacent, requires compiler-specific ptr_cast workarounds, and forces manual destructor tracking), yet reflect-cpp offers no public, exception-free, fallible struct-construction primitive for custom deserializers.

Suggested fix: Expose a public helper such as rfl::try_construct<T>(field_fn) / rfl::make_from_fields<T>(field_fn) returning rfl::Result<T> that invokes field_fn(field_descriptor) for each reflected field 0..N-1, short-circuits on the first rfl::Error, cleans up already-constructed fields cleanly (or aggregates std::optional/tuple storage safely without UB on uninitialized T), and constructs T without requiring default-constructibility or exceptions.

Activity

  1. added
    enhancement
    needs-triage
    known-downstream-workarounds
    on Nov 29, 2026
  2. added
    wontfix
    and removed
    needs-triage
    on Nov 29, 2026
  3. clockblocker commented on Nov 29, 2026

    @clockblocker
    OwnerAuthor

    Triage decision: closing as wontfix (not planned) for the upstream adoption track.

    Rationale: sqlgen-only internal fallible in-place struct builder (ptr_cast / placement-new / destructor rollback). sqlgen is authored by reflect-cpp's own maintainer; if wanted in rfl, he can promote it directly. Zero external adoption leverage.

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

    enhancementknown-downstream-workaroundswontfix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions