Repository navigation
Description
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:
- Either re-implement raw aligned byte storage +
ptr_cast+rfl::to_viewon uninitialized memory + placement::new+ manual destructor rollback on error and after move; OR - Construct
rfl::named_tuple_t<T>(read_field<is>(...).value()...)via throwing.value()inside atry/catchblock because there is noResult-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:with placement-new ontemplate <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; }
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 commitd3de060,from_str_vec.hppcalledrfl::internal::ptr_cast<T*>(&buf)directly; ind3de060,sqlgencopiedrfl::internal::ptr_castverbatim 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: duplicatesreflect-cpp's manual per-field destructor loop overViewType::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
clockblocker commentedon Nov 29, 2026
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).sqlgenis authored byreflect-cpp's own maintainer; if wanted inrfl, he can promote it directly. Zero external adoption leverage.