• Stars
    star
    116
  • Rank 303,894 (Top 6 %)
  • Language
    C++
  • License
    Creative Commons ...
  • Created about 7 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Future C++ implementation of Rust-like trait objects with no boilerplate

Typeclasses in C++

This is an idea of how Rust-like trait objects could be implemented in C++ using static reflection, code injection and metaclasses. This solution is very succinct, requiring no boilerplate or arcane tricks. It has been tested against the Clang fork at https://gitlab.com/lock3/clang.git 02eaac5aa06dfa0d19de95270bccd6311f11f5ba.

The idea is to use class definitions which have only declarations of members as typeclasses. For example, a typeclass which is satisfied by classes with a print function which returns nothing and takes no arguments can be defined as follows:

class(typeclass) printable {
    void print();
};

The typeclass metaclass will generate a class which can be constructed from values of any type satisfying the interface. The given object will be type-erased and forwarding functions will be generated to call the relevant functions on the implementation.

Say that we have a couple of classes which fulfil this interface:

struct foo {
    void print() { std::cout << "foo"; }
};

struct bar {
    void print() { std::cout << "bar"; }
};

We can type-erase these objects by using printable:

printable a = foo{};
printable b = bar{};
a.print();
b.print();

We could even store them in a container:

std::vector<printable> v;
v.push_back(foo{});
v.push_back(bar{});
v[0].print();
v[1].print();

Or reassign from different types:

printable a = foo{};
a.print();
a = bar{};
a.print();

As support in the compiler progresses, I'll think about adding opt-in features, or C++11-concepts-style concept maps.

See Sean Parent's talks on Inheritance is the Base Class of Evil and Value Semantics and Concepts-based Polymorphism for some of the motivation behind this. See Dyno and Boost.TypeErasure for other implementations.

More Repositories

1

expected

C++11/14/17 std::expected with functional-style extensions
C++
1,307
star
2

optional

C++11/14/17 std::optional with functional-style extensions and reference support
C++
793
star
3

minidbg

A mini x86 linux debugger for teaching purposes
C++
507
star
4

vizh

An esoteric visual language that takes image files as input based on a multi-tape turing machine, designed for compatibility with C.
Python
229
star
5

function_ref

A lightweight, non-owning reference to a callable.
C++
168
star
6

cpp-documentation-example

An example of setting up Sphinx for C++ and building with CMake and Read the Docs
CMake
85
star
7

actions-eleventy

GitHub Action for generating a static website with Eleventy
Shell
84
star
8

non-binary

πŸ’›β™‘β™₯πŸ’œ
Isabelle
76
star
9

ranges

Ranges that didn't make C++20
C++
73
star
10

generator

Single-header, ranges-compatible generator type built on C++20 coroutines
C++
57
star
11

tl

A bunch of small C++ utilities
C++
54
star
12

caginator

TypeScript
43
star
13

secret-hitler-strategies

Unofficial Secret Hitler strategy guide
HTML
34
star
14

etkf

Embarrassingly templated keyboard framework
C
32
star
15

enjamb

An esoteric programming language where it's not what's in your lines that matters β€” it's where you break them.
C++
29
star
16

dltrace

A demonstration of tracing dynamic library loading and unloading on Linux.
C++
15
star
17

tartanllama.github.io

CSS
15
star
18

piscrn

A screenshot server, library, and command-line utility for the Raspberry Pi
C
12
star
19

advent-of-code-2020

Rust
11
star
20

lameduck

Terrible Brainfuck to x64 compiler developed live in an hour
C++
11
star
21

aoc-2021

C++
7
star
22

writing-a-linux-debugger

7
star
23

icu-emscripten

Binaries and headers for ICU built for Emscripten
C
7
star
24

aoc-2022

C++
5
star
25

koura

A C++ text template engine.
C++
5
star
26

cppquiz17

Porting questions on cppquiz.org to C++17
C++
4
star
27

rustcpp

Rust
4
star
28

tl-docs

Documentation for the tl libraries
Python
4
star
29

jork

C++
4
star
30

li1I

A domain-specific programming language for integer calculations. Includes LLVM-based compiler.
C++
4
star
31

cppcon-cmake

CMake
3
star
32

cpppaperbot

Python
3
star
33

amer

A C++17 static website generator
CSS
3
star
34

tl-cmake

CMake
3
star
35

advent-of-code-2018

C++
3
star
36

retroman

LaTeX template for writing retro-style technical manuals
TeX
3
star
37

pet_bee

Lua
3
star
38

raytracer

Simple C++ raytracer
C
2
star
39

secrethitler.tv

Secret Hitler on your TV!
JavaScript
2
star
40

sybrand.ink

HTML
2
star
41

shotts

C++
2
star
42

codeart

Sass
2
star
43

elfheadedit

Small ELF file header editor
C++
2
star
44

elmscrew

An online brainfuck debugger
Elm
2
star
45

trashheap

an online """""art"""" journal whomst uploads trash
JavaScript
1
star
46

actions-eleventy-test

1
star
47

caginator-test

1
star
48

azure-keyvault-encryption-test

C++
1
star
49

actions-test

C++
1
star
50

presentation_optional_expected

CSS
1
star
51

cpp-con

C++
1
star
52

cpphalloffame

HTML
1
star
53

tartanllama.xyz

HTML
1
star
54

constraint-solver

[OLD] A constraint solver
Common Lisp
1
star