This repository is an experimental implementation of runtime polymorphism based on structural traits and type-erased trait handles.
#include "trp/shared_trait_ptr.hpp"
struct drawable {
void draw() const;
};
struct circle {
void draw() const {};
};
static_assert(trp::any_trait<drawable>);
static_assert(trp::implements_trait<circle, drawable const>);
void draw_object(trp::shared_trait_ptr<drawable const> ptr) {
ptr->draw();
}
int main() {
auto p = trp::make_shared_trait<drawable const, circle>();
draw_object(p);
}A TRP trait is a class type accepted by the trp::any_trait concept. A trait
must satisfy these restrictions:
- all members and base classes are public;
- no virtual functions or virtual bases;
- no non-static data members;
- no non-default special member functions;
- no operators;
- no non-static template methods;
- no rvalue-qualified methods or explicit-object-by-value methods;
- static data members are accepted only when recognized as
constexpr. The current check is GCC-specific. Clang effectively rejects static data members; - static functions are allowed only when they are templates used as default implementations for trait methods.
trp::implements_trait<Impl, Trait> is satisfied when every method in Trait
has a suitable implementation for Impl.
Implementation lookup uses this order:
-
trp::impl_spec_for<Impl, std::remove_cv_t<Trait>>specialization;[!NOTE]
trp::impl_spec_for<I, T>requiresTto be cv-unqualified. -
trp::impl_spec_for<Impl, TraitBase>for each base ofTrait;[!NOTE] This search is depth-first at the moment.
-
Public direct members of
Implthat satisfy all of these conditions:- the identifier matches;
- the member is callable through a cv-qualified reference to
Implwith the arguments specified by the trait method; - the invocation is
noexceptif the trait method isnoexcept; - the return type exactly matches the type specified by the trait method.
-
Steps 1-3 are repeated for public bases of
Impl, breadth-first. -
Trait defaults.
Lookup does not perform overload resolution. Full overload resolution would be complex to implement and, as far as I know, is not possible for function templates in C++26.
Basic overload resolution is a desired future improvement:
- build the full viable overload set for each source in the lookup list;
- use a perfect match when one is present.
Return type matching could become a customization point, but it is not implemented at the moment.
The core handle is the type-erased dyn_trait_ref<Trait>. It is a non-owning
view over a trait object.
Method access uses dot notation: ref.foo();. Because dot notation is reserved
for trait method access, operations on dyn_trait_ref itself are provided as
free functions:
trp::is_holding_type<Impl>(ref) -> boolchecks whether the implementation object isImpl.Implmust match cv-qualifiers exactly.trp::trait_cast<ExplicitSupertrait>(ref) -> dyn_trait_ref<ExplicitSupertrait>casts to an explicit supertrait. Any trait vtable includes all direct supertrait vtables, so this conversion is always valid.trp::trait_cast<AnotherTrait, Impl>(ref)returns a trait reference assuming the object isImpl. If the underlying object is not of typeImpl, calling any method is potentially undefined behavior.trp::is_valid_const_trait_cast<CVTrait>(ref) -> boolreturns true when the underlying object implements cv-qualifiedCVTrait.TraitandCVTraitmust have the same unqualified type.trp::const_trait_cast<CVTrait>(ref)returns a trait reference without checking validity. Calling any method after an invalid cast is potentially undefined behavior.
The repository also includes these owning handles:
trp::shared_trait_ptr, a reference-counted trait handle;trp::unique_trait_ptr, a new-allocated non-copyable trait handle;trp::alloc_unique_trait_ptr, an allocator-aware non-copyable trait handle.
The owning handles are currently very basic. They share this API:
explicit operator bool()checks whether the handle stores an object;operator->andoperator*access the underlyingdyn_trait_ref;get() -> void*returns the type-erased implementation pointer;get<Impl>() -> Impl*returns the implementation pointer when the runtime type isImpl, otherwisenullptr;trp::is_holding_type<Impl>(ptr) -> boolchecks the implementation type;trp::trait_cast<ExplicitSupertrait>(ptr)moves or copies to an explicit supertrait handle;trp::trait_cast<AnotherTrait, Impl>(ptr)returns an empty owning handle unless the stored object isImpl.
Specific owning-handle APIs:
trp::make_shared_trait<Trait, Impl>(...)andtrp::allocate_shared_trait<Trait, Impl>(alloc, ...);trp::make_unique_trait<Trait, Impl>(...)andtrp::allocate_unique_trait<Trait, Impl>(alloc, ...);trp::unique_trait_ptr<Trait>can be moved intotrp::alloc_unique_trait_ptr<Trait>.
Currently, trp does not have a clear way to define a custom owning handle.
- Definition of traits
- Normal methods
- cv-qualified methods
-
noexceptqualification - Trait inheritance
- Default implementations, inline and explicit
- Explicit specialization of implementation methods
- Concepts:
any_trait: checks whether a type is valid as a trait definition;implements_trait: checks whether a type implements all methods of the trait;supertrait_of<S, T>: checks whether the method set ofSis a subset of the method set ofT;explicit_supertrait_of<S, T>:supertrait_of<S, T>andSis in the inheritance chain ofT; true forS == T;direct_supertrait_of<S, T>:supertrait_of<S, T>andSis a direct base class ofT; false forS == T.
- Non-owning type-erased trait handle
dyn_trait_ref<T>-
dyn_trait_ref<cv_trait>wherecv_traitis cv-qualified - Upcasting to
explicit_supertrait_of<S, T>viatrait_cast<S> - Runtime type identification for implementations via
bool trp::is_holding_type<Impl>(const dyn_trait_ref<T>&) - Casting to other traits by providing the implementation type via
trait_cast<T, Impl>. This is a checked cast for owning handles and an unchecked cast fordyn_trait_ref. - (?) Conversion to non-explicit supertraits for allocator-aware handles by constructing vtables at runtime
-
- Basic owning trait handles
-
shared_trait_ptr,unique_trait_ptr, andalloc_unique_trait_ptr -
make_shared_trait,allocate_shared_trait,make_unique_trait, andallocate_unique_trait
-
C++26 reflection cannot generate types with methods. It can only generate aggregates with public data members.
The methods of a polymorphic trait object are emulated by data members of type
method_invoker<...> with the [[no_unique_address]] attribute and operator().
/* method-holder */ type is a standard-layout class with a first and only data member of type method_invoker<...>.
/* method-holder */ is defined via std::meta::define_aggregate to generate "methods" with the correct identifiers.
dyn_trait_ref_impl<...> is derived from all required holders and stores a vtable pointer and a type-erased object pointer.
/* dyn-ref-wrapper */ is derived from dyn_trait_ref_impl<...>.
To access the vtable and object pointers from inside operator(), the following
chain of casts is performed:
- The
thispointer ofmethod_invoker<...>::operator()(...)isreinterpret_castto a/* method-holder */pointer. This is well-defined because/* method-holder */is a standard-layout struct with a single member. - The
/* method-holder */pointer isstatic_castto a/* dyn-ref-wrapper */pointer. This is well-defined because of inheritance.
The vtable and type-erased object pointers are then acquired through the
/* dyn-ref-wrapper */ pointer.
Because access happens through a proxy, the cv-qualifiers of
dyn_trait_ref<Trait> must not be transient and should be inferred from
Trait. To achieve this, cvm_invoker<...> provides
operator()(auto const* vtable, void* obj, <method arguments>) with the correct
cv-qualifiers for each overload. A cv-qualified cvm_invoker<...> with
qualifiers equal to the Trait qualifiers is constructed and invoked.
This uses native C++ overload resolution for both argument types and cv resolution.
Compilation is relatively slow. Some effort was made to minimize repeated evaluation in the implementation.
Compilers may require -fconstexpr-steps with a high value to compile
successfully. static constexpr trait data members are supported only by GCC.
clangd works but has significant delays when handling trait objects.
Some patterns in the trp implementation could be updated to more modern and
cleaner versions with additional C++26 features as compiler support matures.
Given the early stage of reflection compiler and tooling development, these issues may improve over time. However, current tooling challenges raise concerns about possible production usability.
Not implemented, but potentially feasible and interesting:
- Partial overload resolution of implementation methods during vtable construction
- Definition of trait combinations, such as a greatest common supertrait or a common subtrait without inheritance and explicit definition
- Option for return type conversion in implementation methods
- Small object optimization
- (?) Non-type-erased reference wrapper to enforce restricted interfaces
At this moment, the repository is for experimenting and sharing.
- Configure with the p2996 preset:
TRP_P2996_INSTALL_PATH=/path/to/clang-p2996/install cmake --preset clang-p2996 - Configure with the GCC preset:
cmake --preset gcc - Build with the matching build preset:
cmake --build --preset <clang-p2996|gcc>