Qué hace
- <code>ETL_META_DESCRIBE</code> con atributos (<code>Range</code>, <code>Default</code>, <code>Sensitive</code>)
- Recorrido de campos (<code>ForEachField</code>)
- Nombres de enums y parseo
- <code>TypeName<T>()</code> idéntico en MSVC y GCC
- Describe tipos de terceros sin modificarlos
- Registro en tiempo de ejecución separado
Medido
- Struct de 50 campos: el recorrido genera el mismo ensamblador que el acceso directo, 52 instrucciones en GCC 15.2 y 51 en MSVC 19.51
- Struct de 50 campos: tiempo de compilación extra por unidad de traducción de +42 ms en GCC y de +173 a 180 ms en MSVC (objetivo 200 ms, con un margen estrecho: una ejecución con ruido llegó a 261 ms)
Límites conocidos
- Config, ECS y Event aún no usan Meta, por diseño
Ethereal::Meta · Tier 0, prerequisite
Headers only, a leaf (no other Ethereal module is included), C++17, standard library only. No RTTI, no typeid, no allocation.
---
Why it exists
Five specs each described their own macro for binding the fields of a struct to names (Config, Serialization, RPC, ECS, FormaUI): five syntaxes to learn and five places for the same defect to live. Meta is the single one.
---
Usage
#include <ethereal/cpp/meta/meta.hpp>
namespace M = Ethereal::Meta;
struct SServerConfig { uint16_t iPort; std::string_view sBindAddress; bool bEnableTls; };
ETL_META_DESCRIBE( SServerConfig,
ETL_META_FIELD( iPort, "port", M::Range( 1, 65535 ), M::Default( 8080 ) ),
ETL_META_FIELD( sBindAddress ), // derived name: "bind_address"
ETL_META_FIELD( bEnableTls, M::Sensitive() ) );
M::ForEachField( cfg, []( std::string_view name, auto & value, const auto & attrs ) {
if constexpr( ETL_META_HAS( attrs, M::Sensitive ) ) { /* mask it */ }
} );
enum class ELogLevel { Trace, Debug, Info, Warn, Error };
ETL_META_ENUM( ELogLevel, Trace, Debug, Info, Warn, Error );
M::Name( ELogLevel::Warn ); // "Warn"
M::Parse( "Warn", &eLevel ); // true
M::TypeName< ns::Foo< int, ns::Bar > >(); // "ns::Foo<int, ns::Bar>" on the measured compilersRules worth knowing up front
- The macro is invoked in the namespace of the type. The descriptor is found by ADL. To describe a third-party struct, reopen its namespace:
namespace vendor { ETL_META_DESCRIBE( SPacket, ... ); }. A type with a comma in its name (a template with two arguments) must go through ausing. - The derived name: a Hungarian prefix of one or two lowercase letters is dropped (
iPort→port,szName→name), and the rest becomessnake_case(sBindAddress→bind_address,uMaxHTTPRetries→max_http_retries). Three lowercase letters are not a prefix:maxValue→max_value. Two fields that derive the same name (iValueandpValue) are rejected at compile time, with a one-sentence message. Give one of them an explicit name. - Custom attributes derive from
Meta::SAttribute. That base is howETL_META_FIELDtells an attribute apart from the explicit name. - Querying attributes at compile time:
ETL_META_HAS( attrs, Meta::Sensitive ). It usesdecltype, so it is a constant on any compiler. A directif constexpr( Has( attrs ) )is only a constant on the compilers that already implement P2280 (C++23). - Limits: 128 fields or more per struct (the fields are never counted by the preprocessor), and 125 enumerators per
ETL_META_ENUM. MSVC rejects macros with more than 127 parameters (error C1112, measured), and the counting needs two more. Two descriptions on the same line of the same namespace collide with a compile error (the descriptor name uses__LINE__instead of__COUNTER__, because the counter would differ between translation units and break the ODR).
---
Two headers, on purpose
| Header | Contents | Runtime cost |
|---|---|---|
meta.hpp | description of structs, enums, flags and services. Attributes. TypeName. Traits | zero |
meta_runtime.hpp | dense TypeId<T>() and the type-erased registry Fields( id ) | one atomic counter and one table |
meta_runtime.hpp stays out of meta.hpp because it has state and needs <atomic>, which costs ~90 ms of build time per translation unit on MSVC. Code that needs ids or the template-free boundary (ECS, Event, C layers) includes it. Code that only describes types does not pay for it.
**TypeId is dense and assigned on first use.** A dense integer, unique per type, fixed at compile time across translation units, does not exist in portable C++. What is delivered is what the consumers need: dense from zero, stable for the life of the process, usable as a vector index. It is not the same number between two runs. For a key that is stable across runs and binaries, use TypeName. In a DLL, each module has its own counter.
---
Measurements
All reproducible: tools/meta_check/check.sh (GCC, on Linux/WSL) and tools/meta_check/check_msvc.ps1 (MSVC, with the vcvars64 environment). The probe is a 50-field struct (tools/meta_check/probe_50.cpp). The reference mode is the hand-written equivalent, term by term (t += s.field).
| Spec criterion | GCC 15.2 | MSVC 19.51 | Clang |
|---|---|---|---|
| Runtime cost: same assembly as direct access | ✅ 52 identical instructions | ✅ 51 identical instructions (it was 99: see below) | not measured |
| Constant descriptor, no static initializer | ✅ no _GLOBAL__sub_I / .init_array | ✅ no .CRT$XCU | not measured |
Build < 200 ms extra per translation unit (50 fields, -O2//O2) | ✅ +42 ms (header: +10) | ✅ +173 to 180 ms (header: +9 to 21) | not measured |
There is no Clang on this machine. The spec asks for all three compilers, and Clang was not verified. The TypeName normalization follows the Clang form by design, but this was not measured.
How the MSVC numbers got there (measured, with the harness that checks each build): +423 ms → remove <array> and <atomic> from meta.hpp (−162) → check for repeated names with a hash table instead of all pairs (−~50) → read the tuple leaves with no functions in between and no intermediate lambda (−~90) → +172 ms. The __forceinline on the ForEachField chain is what removes the call on MSVC, whose inliner gave up on the fold of 50 calls on its own.
How the MSVC assembly became identical (batch 3, second pass). The difference was not in the fold or in the lambda: in a separate experiment, with the same number of fields, MSVC generated the direct access with the fold, with recursion and with the lambda. It was in how the member pointer arrives. Read from the descriptor (Object.*( Field.pMember )), even as a constant, MSVC loaded each field into a register before adding (mov + add, 99 instructions). Passed as a template argument, it comes out instruction for instruction identical to the hand-written sum. ForEachField now receives the descriptor as a type and applies Object.*kMember< P > (a variable template). The first version, with a function Membro< P >( o ), cost ~40 ms more build time on MSVC (225 ms, above the ceiling). The variable template, with the tuple leaves read by static_cast, went back to 173 to 180 ms. Measured in the same sessions: the previous version, 179 to 187 ms.
---
Implementation status
| Criterion (spec §5) | Status |
|---|---|
| Zero runtime cost, identical assembly | ✅ GCC (52 instructions) · ✅ MSVC (51), checked instruction by instruction by the verifica* scripts |
Descriptor in .rodata, no static initializer | ✅ GCC and MSVC |
| Build < 200 ms/TU with 50 fields | ✅ GCC +42 ms · ✅ MSVC +173 to 180 ms (narrow margin: the noise on this machine has already produced one measurement of 261 ms, with the header alone at 53) · Clang not measured |
TypeName identical on all three compilers | ✅ identical on MSVC and GCC (16 cases in static_assert, including nested templates, long long, unsigned long, anonymous namespace) · Clang not measured |
Dense, stable TypeId, usable as a vector index | ⚠️ dense, stable within the process. Assigned at runtime, not at compile time (see above) |
Enum Parse compiles to a switch | ⚠️ compares the length before the bytes. The assembly was not inspected, and the claim "it becomes a switch" is not made |
| One-sentence error messages, with compile-failure tests | ✅ four cf.meta.* tests (undescribed type, repeated name, attribute that does not derive from SAttribute, undescribed enum) require the sentence in the compiler output, on MSVC and GCC. A control (cf.control.compiles) proves that the mechanism fails when the file compiles |
| Describing a third-party type without modifying it | ✅ Vendor::SPacket in the test, described from outside |
| 128 fields at minimum | ✅ MetaWide::SWide (128 fields) in the test. Enums: 125 (MSVC limit) |
| Consumers migrated (Config, Serialization, RPC, ECS, FormaUI) | ❌ not done, by decision of the project owner (2026-09-25). ETL_CONFIG_BIND, the ECS ComponentId<T> and the two Event TypeIds remain as they were. The spec says that, without the migration, Meta adds a sixth mechanism instead of replacing five, and that is true until the migration is done |
| UBSan | ✅ meta.cxx20 and meta.cxx17 clean under ASan+UBSan (GCC 15.2, -fno-sanitize-recover=undefined), zero reports |
The tests are tests/meta.cpp, compiled twice (meta.cxx20 and meta.cxx17), without linking the library and without the precompiled header. That is what proves Meta is a C++17 leaf. They compile with warnings as errors on both compilers.
---
C layer
There is none, on purpose, and there is no ethereal/c/meta/ folder. Meta describes C++ types at compile time: the description comes from templates and from constexpr over the annotated struct, and the meta_runtime.hpp registry only knows types that C++ code has registered. A C struct goes through none of this, and a C layer could only return descriptions that C itself cannot create. Code that needs fields by name in C writes the table by hand (name, offsetof, type), which is what Meta does by template in C++.
---
Non-goals
Runtime type registration, runtime method invocation by name, inheritance reflection, code generators, automatic description without annotation.