11 ms·
I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's alread
by blazemaster 7y ago
I don't really understand what the point of all this is, and why anyone would consider using this in a real-world application they're developing. There's already a C-like language which lets you write type-safe generic programs: it's called C++! And if for some reason you really want or need to restrict yourself to using C language features (almost) exclusively, you can disable RTTI, exceptions, and use a technique to avoid linking to libstdc++ (or equivalent). It really seems to me that the point of all this is just to reinvent the wheel, but maybe I'm missing something, I only skimmed the paper.
- billfruit 7y agoI also think it is rather strange for many projects to continue with C, esp for large projects, where c++ offers much conveniences to the programmer. IIRC gcc only recently started allowing c++ in it's source, and I wonder if emacs allows it still in it's source.
- rootw0rm 7y agoSQLite kicks ass. Don't hate.
- aidenn0 7y agoExceptions are a major point of contention. Most of the conveniences C++ offers to the programmer are not possible without exceptions, and non-local transfer of control is not just a huge departure from C, it's one that is hard to gradually transition to.
- edflsafoiewq 7y ago> Most of the conveniences C++ offers to the programmer are not possible without exceptions Like what?
- aidenn0 7y agoPretty much anything involving constructors that fail.
- edflsafoiewq 7y agoIt's easy to not write constructors that can fail. The major conveniences like templates, RAII, overloading, algorithms, lambdas, virtual functions, etc. work fine without exceptions.
- Koshkin 7y agoWhile the "major conveniences" are orthogonal to exceptions, constructors are not, and in many non-trivial cases constructors should be expected to fail.
- aidenn0 7y agoIt's doable to write constructors that don't throw exceptions; writing constructors that can't fail is not possible for a large fraction of the useful space of constructors. If your constructors fail without throwing an exception, much of the usefulness of RAII goes away.
- edflsafoiewq 7y agoThe traditional approach would be to have an empty state that the object can be put into, ie. classic two-step initialization. This isn't usually too hard since you often need an empty state you can put that kind of object into when it's moved from anyway. Another one is to have a private constructor called from a static function returning a std::expected<your_class, error>. How would that make the usefulness of RAII go away?
- aidenn0 7y agoThe whole point of RAII is that the scope of the variable is identical to the liveness of the resource. Two step initialization makes that no longer true.
- deleted 7y ago[deleted]
- bluetomcat 7y agoIt seems to be aimed at quick prototyping of small programs, relieving you from declaring any structs or datatypes at all. The fields and layout of structs are inferred from the usage. With their frontend, you would be able to implement a type-generic list or a tree only with a few function definitions. Looking at their example: _Generic void prepend(_Forall(node_t)** head, _Forall(value_t) value) { node_t* n = malloc(sizeof(node_t)); n->value = value; n->next = *head; *head = n; } For each use of `prepend()`, their frontend defines an appropriate struct with fields `value` then `next` (in that order) whose types are inferred from the assignments in the body of the function.
- Crinus 7y agoThe problem with C++ is that in addition to generic functionality, it also brings a ton of other baggage that even if you do not use, you still pay for (e.g. personally i really dislike how slow C++ compilers are). Though TBH i wouldn't use OP's preprocessor either as i think that void* and macros are perfectly fine.
- klingonopera 7y agoExceptions being one of the notable pieces of baggage...
- Crinus 7y agoYeah, though i'm not sure if exceptions really slow down compilation and in some compilers you can disable them anyway. Also you can redefine new to return null on failure, though if you are going to use new, delete, etc might as well bite the bullet and use the entire language anyway (and instead of redefining new, use a template that does the allocation and initialization based on some common convention and a macro that causes an error whenever new is used since you can ensure no exceptions will be thrown... at least from new - but IMO a better way is to just stick with C and avoid the tarpit that is C++).
- klingonopera 7y ago...my compile times rarely take more than a minute, so I can't really relate to that, but exceptions will slow down execution and add an overhead to the binary's file-size.
- wvenable 7y agoExceptions don't generally slow down execution -- they usually have zero runtime cost if they're not thrown making them better than error returns for performance. However, the exchange is, as you said, that they do add a lot of overhead to the file-size.
- 7y ago
- 19ylram49 7y agoNo, I’m not going to drag in C++ and all of its baggage if all I need is some form of generics. It’s really not as simple as, “Just use C++!” If I truly need type-safe generics and C11 is an option, I’d rather go with that any day!
- platform 7y agoOne can also use D (now part of GCC). D with its better dialect allows one to not use D run time libraries, or even C++ runtime libraries. A developer can just choose what C libraries they want to link with. In this mode, no GC is provided, so a developer will be using manual allocation. With upcoming D's dip1000 feature, it will be able to do rust-like borrows checker at compile time (or at least this is my understanding). And that capability will be available in betterC mode as well (not just for full blown D). The code in betterC can use generics and other advanced features https://dlang.org/spec/betterc.html https://dlang.org/spec/betterc.html --- Nearly the full language remains available. Highlights include: - Unrestricted use of compile-time features - Full metaprogramming facilities - Nested functions, nested structs, delegates and lambdas - Member functions, constructors, destructors, operating overloading, etc. - The full module system - Array slicing, and array bounds checking - RAII (yes, it can work without exceptions) scope(exit) - Memory safety protections - Interfacing with C++ - COM classes and C++ classes - assert failures are directed to the C runtime library - switch with strings - final switch - unittest dip1000: https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000...
- okl 7y agoGenerics != templates