Re:Writing your own C++ standard library from scratch
by account42 on Tuesday March 25, 2025 @12:04PM
(#43472890)
Note that even for C the dependency from compiler to standard library exists in practice because optimizing compilers will treat some standard library functions like memcpy specially by default and either convert calls to them into optimized inlined code, generate calls to them from core language constructs, or otherwise make assumptions about them matching the standard library specification. And beyond that you need compiler support libraries for things like operations missing from the target architecture or stack probes required on some platforms and various other language and/or compiler features.
But for all of these (including the result types of operator<=>) you can define your own version so it's a rather weak dependency.
Re:Writing your own C++ standard library from scratch
by pjmlp on Wednesday March 26, 2025 @04:55AM
(#43480153)
> C++ inherited from C a pretty strong distinction between "language" and "library".
This is long gone in ANSI/ISO C, as there are language features that require library support, like floating point emulation, followed by threading, memory allocation (tricky without using Assembly), among others.
Which is why freestanding subset exists, or one has to otherwise call into OS APIs as alternative to standard library, like it happens on non-UNIX/POSIX OSes.
Your example with partial_ordering is actually one of my longstanding pet issues. It would have been possible (I wrote in https://quuxplusone.github.io/blog/2018/04/15/built-in-libra... ) to define
But it remains impossible, AFAIK, to define `weak_ordering` from within the core language. Maybe this is where someone will prove me wrong!As of C++14 it's even possible to define the type `initializer_list` using only core-language constructs:
(But you aren't allowed to do these things without including <compare> resp. <initializer_list> first, because the Standard says so.)