Omnirefl
A C++ reflection tool built for a seamless experience without macros* or UB. (C++11 minimum.)
* optional QoL and compatibility
Obligatory self-reflection meta joke.
Seamless Experience
- Add
omni_reflected_target(...)for a CMake target. - Use
omni::reflected_call(...)where reflection is needed.
Everything else remains regular C++. No macros*, compiler-specific tricks,
undefined behavior, or manual regeneration are required.
* Reflection uses AST instrumentation. Macros are optional conveniences or compatibility fallbacks.
Types can be declared and reflected directly in the same .cpp. No dedicated
declaration headers, schemas, annotations, or checked-in metadata files are
required; generated metadata remains a build artifact.
Sneak Peek
A simple recursive type reader explores discoverable dependencies and prints their metadata. The complete runnable example also demonstrates modifying fields through reflection.
// One .cpp; no declaration headers, metadata files, or reflection macros.
/** This example intentionally uses C++23 syntax and library facilities to
* reduce verbosity. The reflection APIs also support C++11. */
void print_indented(std::string_view text);
std::string format_annotation(std::string_view annotation, unsigned indent = 0);
/** Selected metadata queries used by print_reachable_records below.
* This (optional) namespace marks helpers that must not be used outside
* a reflected scope. Instrumentation checks out-of-scope queries
* (best effort). */
namespace reflected_scope {
// Showcase data-member metadata queries within a reflected scope.
std::string format_qualifiers(omni::data_member_meta auto field) {
constexpr auto format_bitfield = [](omni::data_member_meta auto field) {
if constexpr (field.is_bitfield())
return std::format("bitfield({}) ", field.bit_width());
else
return "";
};
// Metadata names, type names, and property queries are constexpr.
return std::format("{}{}{}{}{}",
field.is_static() ? "static " : "",
field.is_const() ? "const " : "",
field.is_mutable() ? "mutable " : "",
field.is_volatile() ? "volatile " : "",
format_bitfield(field));
}
std::string format_enum(omni::enum_meta auto e) {
using namespace std::string_view_literals;
constexpr auto enumerator_name = [](auto enumerator) {
const auto &[value, name] = enumerator;
// Enumerator names are string literals with static storage.
return std::string_view{name};
};
return std::format("[{}]",
e.enumerators()
| std::views::transform(enumerator_name)
| std::views::join_with(", "sv)
| std::ranges::to<std::string>());
}
// Non-empty documentation includes its separating line break.
std::string format_documentation(auto meta) {
const std::string_view text = meta.documentation();
return text.empty() ? "" : std::format("{}\n", format_annotation(text));
}
template <typename ValueMeta>
requires (omni::data_member_meta<ValueMeta>
|| omni::function_param_meta<ValueMeta>
|| omni::function_return_meta<ValueMeta>)
std::string format_type_name(ValueMeta meta) {
if constexpr (omni::is_reflected<typename ValueMeta::type>::value)
return std::format("{} (resolves to {})",
meta.spelled_type_name(),
// qualified_type_name() also includes enclosing namespaces.
omni::meta_for<typename ValueMeta::type>::type_name());
else
/** Fall back to the data member's or function parameter's source spelling
* when its type is not reflected. */
return meta.spelled_type_name();
}
std::string format_function_param(omni::function_param_meta auto param) {
const std::string_view name = param.name();
const std::string_view documentation = param.documentation();
return std::format("{}{}{}",
format_type_name(param),
name.empty() ? "" : std::format(" {}", name),
documentation.empty() ? "" : std::format(" [{}]", documentation));
}
/** Render an available default value.
* ```cpp
* int make_value();
*
* struct defaults {
* int arithmetic = 8 * 100 + 15; //< default_value() available
* int computed = make_value(); //< default skipped with a warning
* };
* ``` */
template <omni::data_member_meta Field>
requires (Field::has_default_value_access())
std::string format_default_value(Field field) {
return std::format("{}", field.default_value());
}
// Defined below the model to keep formatting glue out of the query examples.
template <omni::data_member_meta Field>
std::string format_field(Field field);
// Defined below the model to keep formatting glue out of the query examples.
std::string format_function(omni::function_meta auto func);
void print_members(omni::record_meta auto record) {
namespace fn = omni::fn;
record.accessible_data_members()
| fn::each([](omni::data_member_meta auto field) {
/** Special case: `omni::function_t` allows reflecting free functions
* "bound" as data members. */
if constexpr (field.has_callable_metadata())
print_indented(format_function(field.callable_metadata()));
else
print_indented(format_field(field));
});
record.accessible_member_functions()
| fn::each([](omni::function_meta auto func) {
print_indented(format_function(func));
});
}
} // namespace reflected_scope
/** Print reachable records depth-first using the query helpers above.
* The traversal is implemented below. */
struct print_reachable_records {
template <omni::record_meta Root>
void operator()(Root) const;
};
template <typename T>
void print_reflection() {
/** `reflected_call` makes metadata for `T` and its type dependencies
* available inside the visitor's `operator()` and the functions it calls.
* The visitor can also be a generic lambda with an explicit return type. */
omni::reflected_call(print_reachable_records{}, omni::type<T>);
}
/** Omnirefl reflects globally accessible, named records (structs, classes, or
* unions) and enums on demand. */
namespace oceanic {
// Declaration comments are available as reflection metadata.
struct vessel { //< Discovered as the `mapped_type` of `fleet<T>::vessels`.
// Nested structs are supported recursively inside non-template records.
struct position { //< Discovered via the `location` data member.
using degrees = double;
degrees latitude = 0;
degrees longitude = 0;
};
position location;
enum class propulsion { sail, motor, hybrid };
propulsion drive{};
// Reflected data-member properties are queryable.
mutable std::string name = "before"; //< `.is_mutable()` is true.
/** Public static and non-static member functions are reflected.
* Parameter and return types join dependency discovery.
* Overloaded member functions are skipped with a warning.
* Doxygen parameter descriptions, such as `@param` or `\param[in]`, are
* available through parameter metadata. */
/** Check proximity.
* @param other Other position.
* @return Whether the positions are near. */
bool is_near(position other) const {
return location.latitude == other.latitude
&& location.longitude == other.longitude;
}
};
/** Measure a distance.
* @param scale Multiplier.
* @return The scaled distance. */
double distance(vessel, double scale) { return scale; }
// Primary templates are supported; names appear without template arguments.
template <typename T>
struct fleet { //< Discovered as an `omni::type_t` argument to `reflected_call`.
// Unsupported nested records emit a warning if discovered as dependencies.
struct not_supported {};
/** Free functions are reflected only when discovered through a data member
* using the special `function_t` tag. */
omni::function_t<distance> measure;
/** Standard-library types are not reflected, but their dependency protocols
* still apply. The specialization for `T` is discovered via `mapped_type`. */
std::map<std::string, T> vessels;
};
struct telemetry { //< Discovered as a value argument to `reflected_call`.
// Writing bit-fields without UB is supported via `.set_value()`.
mutable unsigned depth : 10 = 42;
const unsigned sensor = 108;
// Only public data members are reflected.
private:
[[maybe_unused]] unsigned john_cena = 49; //< can't see
};
} // namespace oceanic
Minimal CMake setup:
cmake_minimum_required(VERSION 3.20 FATAL_ERROR)
project(sneak_peek LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(omnirefl CONFIG REQUIRED)
add_executable(sneak_peek main.cpp)
if(MINGW AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# MinGW's std::print terminal support lives in this library.
target_link_libraries(sneak_peek PRIVATE stdc++exp)
endif()
# Reflection is not transitive: only this target's own C++ translation units are
# instrumented. Call omni_reflected_target for each target that should be
# reflected.
omni_reflected_target(sneak_peek)
Build the example:
# Builds rerun instrumentation automatically for changed inputs.
# To update reflection without compiling:
# cmake --build build --target sneak_peek.omni
cmake --build build --target sneak_peek
Run it:
$ ./build/sneak_peek
// Primary templates are supported; names appear without template arguments.
oceanic::fleet {
// Measure a distance.
measure(vessel (resolves to vessel), double scale [Multiplier.]) -> double;
// Standard-library types are not reflected, but their dependency protocols
// still apply. The specialization for `T` is discovered via `mapped_type`.
vessels: map<std::string, T>
}
// Declaration comments are available as reflection metadata.
oceanic::vessel {
location: vessel::position (resolves to vessel::position)
drive: [sail, motor, hybrid]
// `.is_mutable()` is true.
mutable name: string = before
// Check proximity.
is_near(vessel::position (resolves to vessel::position) other [Other position.]) -> bool;
}
// Nested structs are supported recursively inside non-template records.
oceanic::vessel::position {
latitude: vessel::position::degrees = 0
longitude: vessel::position::degrees = 0
}
before: depth=42 sensor=108 name=before
after: depth=815 sensor=108 name=oceanic
This example is available in the extras archive; see Examples, Tests and Benchmarks.
The comprehensive guide covers the remaining interface and compatibility features.
Status
Omnirefl is under active testing and interface polishing. Version 0.1.0 is planned after the initial set of extensions is complete. Until then, interfaces and package layout may change without compatibility guarantees. Release notes aim to call out every breaking interface change.
Functional Utilities
<omnirefl/functional.hpp> provides compositors such as each, filter,
map, and foldl, including chainable forms:
const auto result = tuple
| omni::fn::filter(predicate)
| omni::fn::map(transform)
| omni::fn::foldl(combine, initial);
Compile-time field predicates compose with &&, ||, and not_:
namespace fn = omni::fn;
constexpr auto non_static_data_fields = fn::is_non_static
&& fn::not_(fn::has_callable_metadata);
// Within reflected scope, given record metadata:
constexpr auto fields = meta.accessible_data_members()
| fn::filter(non_static_data_fields);
The predicates are constexpr. always_true selects every field;
is_assignable checks the declared field type's assignability. For type-based
code, is_non_static_t<Field> remains a Boolean trait.
See the functional tests and comprehensive guide for executable examples and C++11-compatible use.
Install
- Project integration: Conan 2 and CMake FetchContent.
For the fastest drop-in setup, use CMake FetchContent to acquire Omnirefl.
# Package acquisition include(FetchContent) FetchContent_Declare(omnirefl GIT_REPOSITORY https://github.com/sergio-eld/omnirefl.git GIT_TAG v0.0.8 SOURCE_SUBDIR cmake/integration) FetchContent_MakeAvailable(omnirefl) # Project integration find_package(omnirefl CONFIG REQUIRED) omni_reflected_target(my_target)The FetchContent integration is available starting with v0.0.6.
- Release archives: Cosmo universal package for Linux, macOS, and Windows.
- Container builds: Docker Compose or GitHub Codespaces.
- Latest CI builds: workflow artifacts for unreleased changes; availability is temporary.
This page tracks master and may describe unreleased features. Use the
README at your installed release tag when working with a released package.
Examples, Tests and Benchmarks
For an examples sandbox in VS Code or GitHub Codespaces, see the Dev Container setup.
Download omnirefl-extras-<version>-<commit>.tar.gz from the
release matching your Omnirefl package.
This platform-independent archive contains the examples, tests, and benchmarks.
Examples, tests, and benchmarks are enabled by default; disable a category with
-DBUILD_EXAMPLES=OFF, -DBUILD_TESTING=OFF, or -DBUILD_BENCHMARKS=OFF.
Examples whose toolchain requirements are unmet are reported and skipped.
Tests default to the full suite. Use -DOMNI_TEST_SUITE=smoke for the smaller
base-interface suite. Smoke uses each compiler's minimum and maximum supported
C++ standards; full uses every supported standard. Both cover all package
distributions and include guides when supported. PRs use smoke; master
selects smoke or full by the
validation cadence.
Release tags and manual full-suite requests use full validation.
CI compares instrumentation and Release object-build times using the benchmark baseline.
With Omnirefl unpacked into ./omnirefl, configure the extras out of source:
mkdir -p extras
tar -xzf omnirefl-extras-*.tar.gz -C extras --strip-components=1
cmake -S extras -B extras/build -GNinja -Domnirefl_ROOT="$PWD/omnirefl" \
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON # Required by Omnirefl's CMake integration.
cmake --build extras/build --parallel
ctest --test-dir extras/build --timeout 600 --output-on-failure --no-tests=error
Omit omnirefl_ROOT when the package is installed system-wide.
Each extras/examples/<folder> directory is self-contained. Configure one
independently, such as extras/examples/sneak_peek, or copy it into
your own workspace. The Sneak Peek requires CMake 3.20 and a configured
C++23 toolchain.
Extension examples, tests, and benchmarks are grouped under extras/extensions.
Extensions
- Serialization: read and write JSON/YAML using existing C++ types, with diagnostics, field filtering, aliases, and validation. Compatible with C++11; see the guide.
- Further extensions under consideration include RPC mapping, schema generation, and command-line interfaces.
Is It Slow?
The overhead target is roughly the frontend portion of a full object build—around 30%, though AST matching and context resolution can add to it.
Incremental builds
Only instrumented targets pay this cost, and reflected translation units can be isolated in dedicated targets. The cost is most noticeable during initial generation. Dependency files let the build system rerun instrumentation only when the source or an included header changes.
Supported Scope
Omnirefl aims to make it possible to cover the most common reflection needs with a focused feature set and reasonable customization: serialization, RPC mapping, schema generation, and command-line interfaces (see Extensions). Full reflection of the C++ language is outside its intended scope: supporting every language construct would lead to a combinatorial explosion of cases.
Current support covers named C++ records and enums, including public fields and functions. See Limitations for details.
Language: C++11 minimum; C++20 concepts provide the most ergonomic interface.
C++26 support
Native compile-time reflection has been adopted for C++26. Omnirefl is recommended as an interim step until your project can use it.
C++26 support depends on the underlying LLVM/Clang version used to build Omnirefl. Run
omnirefl --versionto see the bundled LLVM version.CMake integration: supports Ninja and Makefile generators, including Ninja with MSVC. Visual Studio generators are not supported.
Records:
- named structs, classes, and unions declared globally or in a namespace
- nested named records inside non-template records
- unconstrained primary record templates with type, non-type, and template-template parameters, including type packs and CRTP bases
Enums:
- named enums declared globally, in a namespace, or inside non-template records
- enumerator names and values in declaration order
Type metadata:
- qualified and unqualified type names
- Doxygen-style documentation
Public data-member metadata and access:
- static and non-static data members accessible from the reflected type, including those inherited through public bases, in declaration order
- field names, declared type names, and documentation
- static/const/mutable/volatile/deprecated traits
- reading and writing field values, including bitfields and misaligned packed scalar fields
- query whether each field can be read or accessed by reference
- default member initializer detection and best-effort access to their values
Public function metadata:
- non-overloaded static and non-static member functions accessible from the reflected type, including those inherited through public bases
- free and static functions represented by
function_tfields; see Limitations - names, documentation, function pointers, arity, parameter metadata, and return metadata
Dependency Protocols
Additional reflected types are discovered through:
- public field types
- public bases and transitive public bases
- public fields of primary template records
- parameter and return types of supported function metadata
- supported public member aliases:
error_typefirst_typekey_typemapped_typesecond_typetypevaluevalue_type
- tuple elements exposed by
omni::tuple_element<I, T>::type, enumerated usingomni::tuple_size<T>::value - variant alternatives exposed by
omni::variant_alternative<I, T>::type, enumerated usingomni::variant_size<T>::value
The traits in <omnirefl/traits.hpp> use the standard tuple/variant protocols
by default and can be specialized for custom types. Extensions register their
MPark.Variant fallback automatically.
See advanced.custom_variant in the
comprehensive guide.
Types declared privately can still be discovered through a public field or a
supported public alias such as value_type.
Standard-library records and types explicitly skipped by the user do not receive metadata. Their supported member aliases and tuple/variant traits still expose dependencies.
How It Works
Omnirefl combines two mechanisms:
- Seamless integration: type-based SFINAE selects generated metadata using forward declarations, without including user declaration headers. Types that can be forward-declared and matched through SFINAE instantiation are therefore potentially supportable. These requirements also set the main limitation: concepts cannot be forward-declared, and unnamed or function-local types cannot be declared in the generated header. Nested types require a usable public path through an enclosing record.
- Discovery through instrumentation: Omnirefl recognizes
reflected_callin the AST, identifies its root records and enums, and follows their public dependency protocols. It emits metadata into a header that is force-included before compiling the translation unit.
A simplified generated shape is:
namespace app {
struct root; // The definition may remain in the translation unit.
}
namespace omni {
namespace detail {
// Field accessors use T, delaying instantiation until app::root is complete.
template <typename T>
struct _reflected<struct app::root, T> {
// Metadata omitted.
};
} // namespace detail
} // namespace omni
Each translation unit gets an internal header tied to its compiler invocation: preprocessor definitions, language and target flags, and include paths. The same source can therefore produce different metadata in different targets. This header is not a reusable interface and does not reproduce user definitions. See Limitations for the declaration restrictions.
Earlier design considerations
Reconstructing user includes was considered, but declarations may exist only in
.cppfiles, library include paths may differ from filesystem paths, and headers may rely on transitive includes or a particular include order.An experimental indexed mode demonstrated reflection without forward-declaring user types, but proved brittle. It registered types during template instantiation, relying on an order the C++ standard does not guarantee. The standard permits some instantiations to be deferred, even when referenced by non-dependent expressions. Discovery and compilation could therefore associate different types with the same index. Matching metadata directly by forward-declared type became the core approach. The indexed implementation was subsequently removed.
Limitations
CMake integration
- Call
omni_reflected_targetfor each target compiling reflection-using sources; instrumentation does not propagate to linked targets. INTERFACE libraries are unsupported. - The wrapper instruments concrete, non-generated C++ translation units. Generated sources are skipped, compiled object inputs are accepted, and other source generator expressions are rejected. C translation units are ignored. If no C++ source remains, reflection is skipped with a warning.
reflected_call
reflected_callis the instrumentation boundary. Its visitor must be a generic lambda or have a templatedoperator(). The return type must be known without instantiating its body during the tool run. Lambdas need an explicit trailing return type, including-> void, and cannot return a type declared inside their body.- Roots must be complete records or enums defined before the call. Other
top-level shapes need dispatch or conversion first; for variants, use
std::visitormpark::visit. - Queries belong inside a reflected scope. Direct recursive
reflected_callis unsupported; a nested call can work only when its reflected path was instantiated independently. Out-of-scope queries are reported as errors on a best-effort basis. - Constant evaluation is supported from C++11 when the visitor and arguments
permit it. During instrumentation the visitor is not invoked; a
default-constructible result is replaced with
Result{}. Compile-time assertions must accommodate that placeholder, particularly for types wherestatic_cast<bool>(Result{})yields false, such asbooland empty optionals.fn::instrumented_validation(check)from<omnirefl/functional.hpp>suppliesstd::true_typeunderOMNI_TOOL_RUNand evaluates the real Boolean check otherwise. The validator andreflected_callremain present in both phases. Apply transformations such asfn::not_to the check before wrapping it. Warning: constexpr reflected_call is intended only for static_assert checks. Do not propagate placeholder results into further compile-time logic. In particular, a non-Boolean result used for template arguments, array sizes, type selection, orif constexprcan make instrumentation compile different code from the final compilation. Keep the validation guard confined to an isolated assertion; it does not make downstream use of a stand-in safe.
Type declarations and dependency discovery
The generated header must name types before their source declarations, which limits the declarations it can represent.
- Local and unnamed types are unsupported as roots. Namespace-scope unscoped enums need a fixed underlying type so they can be forward-declared.
- Records nested inside template records are unsupported.
- Constrained primary record templates and explicit or partial record-template specializations are rejected as roots. Explicit or partial specialization dependencies are skipped with a warning.
- Primary-template support assumes inherited member names and kinds stay the same across specializations. Different inherited member sets, or a member changing between data and function, are unsupported.
- Records with direct or inherited virtual bases are rejected as roots and skipped with a warning as dependencies.
- Public access paths to non-public nested dependencies are not preserved when the exposing field is inherited from a public base. Public nested types inside private enclosing records are also not currently nameable.
- Incomplete dependencies are skipped with a warning, including class-template dependencies whose instantiation requires an incomplete type argument.
- Scalars, pointers, raw arrays, standard-library records, and compound types are not accepted directly as roots. Compound types can still expose dependencies through supported protocols. Invalid-input detection is best effort.
- Pointer/reference pointees and raw-array element types are not discovery routes, even when their definitions are visible.
- Standard-library public bases are ignored. Other unsupported public bases are skipped with a warning, and their inherited fields are omitted.
Metadata and field access
default_value()is generated only when the initializer appears safe to copy into generated metadata. Detection is conservative and best effort: function calls, declaration references,this, macros, and dependent expressions are skipped with a warning.has_default_member_initializer()still reports the declaration;has_default_value_access()reports whether the value is available.- Hidden and ambiguous inherited fields, and fields inherited through non-public bases, are omitted.
- Function metadata excludes member functions with parameter packs, conversion
operators, and immediate functions. Overloaded methods are skipped with a
warning. Only fixed
function_tfield types expose free/static function metadata; dependent function-tag fields in primary templates remain ordinary fields. - Deprecated public fields can emit compiler deprecation diagnostics while
metadata is formed,
before
is_deprecated()can filter them. - Anonymous unions are not reflected correctly.
- Template-dependent bitfield widths are not exposed in metadata. These width queries are disabled with a warning.
- Bitfields and misaligned packed scalar fields do not expose references.
Compiler-packed misaligned raw arrays have no safe whole-field accessor;
use an aligned representation such as
std::arrayfor whole-field access.
JSON/YAML mapping
Records with several recursive fields can require excessive compilation time and compiler memory, even with the tree-depth limit. For example:
struct node {
std::vector<node> children, alternatives, fallbacks;
};
Distinct recursive paths and depths instantiate separate mappings.