r/cpp • u/summetdev • Jun 03 '26
proof of concept c++ runtime & standard library
https://github.com/antsif-a/platinumI've been hanging and experimenting around modern C++ and got plenty of ideas of how c++ standard library could look like. Of course, it sounds like another "c++ stdlib replacement", but see, i think found interesting solutions that could be interesting to you all.
The goal was to make a framework that expects a modern c++ code and compiles it to a very lightweight binary. for example, this code:
import std.io;
int main() {
println("Hello, World");
return 0;
}Compiles to a tiny statically linked 576 byte executable. It does not link either to libc or libstdc++, using a custom runtime (instead of crt), written in fasm.
Another example is an echo server (executable size is 1312 bytes):
import std.io;
import std.net;
import std.string;
import std.view;
int main() {
int sfd = socket(af_inet, sock_stream, 0)
.expect("could not create socket");
setsockopt(sfd, sol_socket, so_reuseaddr, 1)
.expect("could not set so_reuseaddr");
/* host -> network byte order is done at sockaddr_in constructor */
sockaddr_in addr = sockaddr_in(6767, 0);
bind(sfd, addr)
.expect("bind failed");
listen(sfd, 1)
.expect("listen failed");
sockaddr_in peer_addr;
int cfd = accept(sfd, peer_addr)
.expect("accept failed");
string buf = string(128);
for (;;) {
/* read(int, string &) overload sets string length to actual value returned by read */
if (!read(cfd, buf) || size(buf) == 0)
goto close;
write(cfd, buf);
}
close:
close(cfd);
close(sfd);
return 0;
}
iIn both of the examples you can already see particular design choices:
- Modules are first class feature. they speed up compile time and are more convenient to use than headers
- Standard library functions are global, like in C
- Rust-like results instead of exeptions. every syscall wrapper substitutes actual syscalls and returns a struct with a union containing either value or an error (usually an unsigned integer enum)
You can read project philosophy and get more details in the project readme and see another examples here. Currently this project is nothing more than an experiment and just a compilation of some interesting ideas i got lately.
25
u/mjklaim Jun 04 '26
Note that
std.*are modules names reserved by the standard if my memory is correct? If I am correct then your code is not valid C++ at the moment (I suspect compilers dont check that at the moment). Just renaming that module root name would fix it.5
u/playmer Jun 04 '26
This is neat, but that runtime does none of the setup you’d need for using a bunch of C++ language features. Looking at the vcstartup stuff shows how gnarly it can be.
Still though, very neat. I’ve played around with this a few times myself and always stopped myself after getting just a little farther than you are with startup. It’s not an insurmountable task, but one I’ve not decided worth it to me.
3
u/summetdev Jun 04 '26
Another cool idea i found is a use of concepts. We can define a bunch of concepts that require particular rules for template arguments an use them in algorithms. Like this:
17
u/no-sig-available Jun 04 '26
And we all know that this forces all C libraries to add a prefix to their function names. Namespaces was added on purpose.
If nothing else, it saves your program from breaking when the next standard adds 300 new names:
https://eel.is/c++draft/meta.syn
We can also see that, in the example, about every other line is now error handling. With exceptions you can have the happy path look cleaner, and collect all the nasty error handling at the end.
5
u/ignorantpisswalker Jun 04 '26
NOT BAD! Now You only need to re-implement strings, arrays, hash tables, files, sockets, threads, mutex, Windows support, macOS support and you are done!
Regarding the name: just rename the import to "stdx1" and you got also epoch (stdx1.1 for example).
Nice experiment!