A C++26 library for Rust-like dyn traits
This library depends on reflection, which is currently only implemented here, so it is not ready for "real" use yet.
This is a single header library. The source can be copied from include/khct/cpp_dyn.hpp.
It is also designed to be used from CMake, either as a subdirectory or an external project:
include(FetchContent)
FetchContent_Declare(
cpp_dyn
GIT_REPOSITORY https://github.com/KendallHarter/cpp-dyn
GIT_TAG main
)
FetchContent_MakeAvailable(cpp_dyn)
target_link_library(my_target PRIVATE cpp_dyn)#include "khct/cpp_dyn.hpp"
#include <cassert>
// Defining an interface
struct [[=khct::trait]] my_interface {
int get_data() const noexcept;
void set_data(int) noexcept;
};
// Defining a class that implements that interface
struct [[=khct::impl_for<my_interface>]] my_struct {
int get_data() const noexcept { return data_; }
void set_data(int new_data) noexcept { data_ = new_data; }
private:
int data_ = 1;
};
// Using that interface
int take_interface(khct::non_owning_dyn_trait<my_interface> obj) noexcept
{
obj.call(obj.set_data, 20);
return obj.call(obj.get_data);
}
int main()
{
my_struct s;
assert(take_interface(khct::dyn<my_interface>(&s)) == 20);
}See here for more detailed usage.
Explicit this parameters are not supported.
Compilation errors are a nightmare.