Implement the standard C library in C ++

Say the OS / kernel is written with C ++ in mind and does not “make” any pure C-style material, but instead provides a standard C library built on a fully-fledged standard C ++ library. Is it possible? If not, why?

PS: I know that the C library is "part of C ++", but let it be internally based on a C ++ based implementation.

A small update: I think I started discussing what is "allowed" by my rules here. Generally speaking: the implementation of the C Standard library should use C ++, where possible / Right (tm) . I mainly think about algorithms and work on objects of a static class behind the scenes. I do not exclude any features of the language, but instead I try to focus on a reasonable implementation in C ++. As for the setjmp example, I see no reason why a valid C (which will use either others previously implemented in the library parts of C ++ C or not use any other library functions at all) would violate my “rules”. If the C ++ library has no analogues, why discuss its use.

+4
source share
5 answers

I don’t see the reasons why you couldn’t do this, but I also don’t see the reasons why someone would use such an implementation. It will use a lot more memory and be at least slightly slower than a regular implementation ... although it may not be much worse than glibc, whose implementation of stdio is already essentially C ++ anyway ... (Lookup GNU libio ... you will be horrified.)

0
source

Yes it is possible. This would be like one exporting the C API from a library written in C ++, FORTRAN, assembler, or most other languages.

+4
source

In fact, C ++ has the ability to be faster than c in many ways, due to its ability to support many timetime constructs, such as expression patterns. For this reason, C ++ library libraries are generally much more optimized than c, associated with fewer time series, reversal cycles, etc. With new C ++ 0x functions, such as variant templates, the printf function, for example, can be much faster and typical than the version implemented in c. It’s mine you can even draw on the interfaces of many c constructs and evaluate some of their arguments (for example, string literals).

Unfortunately, many people believe that c is faster than C ++, because many people use OOP to mean that all relationships and use should occur through large inheritance hierarchies, virtual dispatch, etc. This led to some early comparisons being completely different from what is considered good use these days. If you use virtual dispatching where appropriate (for example, as file systems in the kernel, where they build vtables via function pointers and often mostly build C ++ in c), you will not pessimize from c and with all the new functions may be significantly faster.

Not only speed can be improved, but there are places where implementation will benefit from better type safety. There are common tricks in c (for example, storing data in void pointers when they should be shared) that provide security like breaks and where C ++ can provide strong error checking. This will not always translate through interfaces to the c library, since they have fixed typing, but it will definitely be useful for library developers and may help in some places where it may be possible to extract additional information from calls by providing as-if interfaces (for example , the interface that void * accepts can be implemented as a common interface with the concept, make sure that the argument is implicitly converted to void *).

I think this would be a great test of the power of C ++ over c.

+4
source

Given that "pure C-material" has such a big coincidence with C ++, I don’t see how you could completely avoid it in everything, and even more so in the kernel of the OS. In the end, this is operation + "pure material C"? :)

However, you could implement some functions of the C library using classes and much more. Embed qsort with std :: sort? Sure, no problem. Just don't forget your extern "C" .

+1
source

Kernels like Linux have a very strict ABI based on syscalls, ioctls, file systems and in accordance with many standards (POSIX is the main one). Because ABI must be stable, its surface is also limited. It would be a lot of work (especially because you also need a minimally useful kernel), but these standards can be implemented in any language.

Edit: You mentioned libc as well. This is not part of the kernel, and the libc language may not be completely related to the kernel, thanks to the aforementioned ABI. Unlike the kernel, libc must be C or have very good ffi for C. C ++ with parts in extern C will match the count.

0
source

Source: https://habr.com/ru/post/1341202/


All Articles