10 ms·
This whole _Generic stuff is a mistake. There is already a descendant dialect of C which has nice overloading, namely C++. Analogy: imagine some people aren't
by kazinator 11y ago
This whole _Generic stuff is a mistake. There is already a descendant dialect of C which has nice overloading, namely C++.
Analogy: imagine some people aren't happy with the new Fortran. Fortran 66 is the best language ever, and the only flavor of Fortran anyone should use ... except: they like a couple of features from Fortran 2008 and Fortran 90.
So, they whip out the Fortran 66 standard and fork it to create "Fortran Classic" where they just adopt what they like from mainstream Fortran --- and in a totally incompatible way!
And that's basically what C is doing now.
- o87dv 11y agoYou comment is a basically a very good example of a strawman argument. Counterargument that destroys your entire premise: C's revisions( note that revisions are not flavors or separate languages ) don't fork.
- kazinator 11y ago> You comment is a basically a very good example of a strawman argument. Is that so? My apologies. What argument have I reduced to a strawman, and what is the proper strong version? > note that revisions are not flavors or separate languages By what definition of "separate"? C11 is in fact a different language that is very similar (and explicitly based upon) C99, and the same holds between C99 and C90. These can be said to be dialects. But by the same token (no pun intended), C++ is also a C dialect.
- nitrogen 11y agoAll of C's revisions retain a coherent philosophy that is incompatible with the philosophy of C++. When C is the right tool, C++ is no more a substitute than Java or C#.
- sjolsen 11y ago>When C is the right tool, C++ is no more a substitute than Java or C# When C is the right tool, assuming that it ever truly is, C++ is a vastly better substitute than Java or C#. C++ uses the same machine model as C; Java and C# do not.
- kazinator 11y agoWhen C++ is not a substitute, it's for some reasons having nothing to do with language or philosophy, such as not having a C++ compiler for the given chip.
- DougMerritt 11y agoThere are plenty of people who strongly disagree with you (Linus Torvalds being the most famous example), so you are stating a matter of personal taste, not of objective fact.
- jcranmer 11y agoC++ incorporates nearly all of C99 directly, save for VLAs, restrict, designated initializers, and a lot of small stuff that won't affect any modern-ish C code (e.g., no implicit int). VLAs and restrict are explicitly not in C++ because they're somewhat broken (C++14 did add "dynamic arrays" which are VLA-lite, and there's work on a better restrict-like functionality, cf. https://isocpp.org/files/papers/N3988.pdf https://isocpp.org/files/papers/N3988.pdf), while designated initializers does not mesh well with C++ semantics. In practice, then, C++ without RTTI, exceptions, or the standard library is basically a strict superset of C and C code compiled as such will tend to be legal and identical in semantics. The same cannot be said for C# or Java. Given that C++ has some rather useful features (most notably, the ability to guarantee cleanup even in the face of inattentive developers), the only real valid reason to not be using C++ instead of C is if there's no C++ compiler.
- kazinator 11y ago> C++ incorporates nearly all of C99 directly, save for VLAs, restrict, designated initializers ... In other words, C++ incorporates nearly all of C90, save for that C99 stuff that wasn't copied from C++! (Or not incompatibly copied.)
- asveikau 11y agoI find the lack of implicit conversion from void * really annoying. Any time I see someone cast the return type of malloc in a .c file, I curse C++ and die a little. This sounds like a trivial thing but it very much changes the feel of the language, namely into one where you need to insert pointless casts to satisfy a whiney compiler.
- danieltillett 11y agoWhy the hate over casting malloc? It really is not a huge issue. A related question is why do people use malloc if calloc is available?
- asveikau 11y agoProbably the best reason to hate it is if you didn't include the right headers and malloc is therefore implicitly defined to return int. That creates a bug on most 64-bit compilers where int is 32 bits. The cast will mask the bug. But more stylistically, let's be honest, something like this is way too verbose: struct foo *bar = (struct foo*)malloc(sizeof(struct foo)); I see code like this often, and I think the only explanation I can think of is that people don't know the language well enough to write: struct foo *bar = malloc(sizeof(*bar)); > A related question is why do people use malloc if calloc is available? Seems like a very different question... calloc multiplies, calls malloc, and zeros. I guess if you don't want to multiply, don't want to zero, don't want to bother specifying another parameter, some combination of these, then malloc might be a better choice. Though I do recall a few years back the OpenBSD folks (possibly others too?) focusing on making sure calloc checks for overflow when it multiplies. [See also: reallocarray]
- pikachu_is_cool 11y agoYou didn't have to give that confusing analogy. I'm sure most people will understand your point without it.
- xamuel 11y agoBut the OP title is misleading, this isn't function overloading at all. It's macro overloading. Subtle but important difference. For example, you can't pass the OP's "add" around as a function pointer. You can't declare its prototype in an outside file (leaving the linker to link it in) without putting its full definition in said outside file. Etc. It also gives, IMO, better error messages than C++. If we take OP's code and change a and b from int to long, we get: test.c:20:24: error: controlling expression type 'long' not compatible with any generic association type printf("%d\n", add(a, b)); // 3 ^ test.c:16:28: note: expanded from macro 'add' #define add(a, b) _Generic(a, int: addi, char*: adds)(a, b) This is better than the situation in C++ where it would say the function does not exist, and then go through every function with that name (possibly quite a lot) and say why each one doesn't work.
- kazinator 11y agoI want to pass a function as a pointer even though it is overloaded. C++ supports that. For instance if we do: int (*p)(int, int) = add; overload resolution kicks in there. I want detailed error messages about overload resolution. Maybe not all the time: give me the executive summary ("no suitable overload of add was found") and the details if I specify some "-Woverload-detailed" option or whatever. That's a quality of implementation matter; neither ISO C nor ISO C++ spell out what diagnostics should look like and how detailed they should be. Token-level macros are hacky cruft that shouldn't be used as the development bed for new features, period. All this stuff ignores 35 years of progress in the C dialect scene alone, not to mention 60 years of progress in general. Above, add isn't even what is understood in computing as a "first class function". If we make it into some hacky macro, we are taking away even that second class status away from it.
- nothrabannosir 11y ago> I want to pass a function as a pointer even though it is overloaded. C++ supports that. That's his point: C _Generic and C++ are not the same. Which one is better was not his point, just that they're different.
- halosghost 11y agoC++ is not just a newer version of C with newer features. It is a completely disparate language. That's not a bad thing (tradition is not always good), but to say that if you want generics, you should just leave C behind is a little misguided.
- ectoplasm 11y agoWith a couple of minor exceptions, C++ is a superset of C.
- halosghost 11y agoIt's not just a couple. There are a large variety of things in C that Cxx does not support at all (e.g., VLAs and _Generic), and many more things that, though they are in both languages behave very differently (e.g., sizeof). It's fine for people to like Cxx, but I do wish we could stop perpetuating the falsehood that C is just a subset of Cxx. Cxx, when it started was a pure superset of C, but that is no longer the case.
- ectoplasm 11y agoAlright, I'll rephrase. There are a zillion differences, but with a couple of minor exceptions, there is very little C++-incompatible C that cannot be straightforwardly expressed as C++-compatible C. I mean, gcc -Wall even turns on -Wc++11-compat and -Wc++14-compat now. Also, sizeof doesn't behave differently, the arguments that get passed to sizeof are different in a few cases, e.g. sizeof('x'). See Annex C: http://www.lcdf.org/c%2B%2B/clauseC.html http://www.lcdf.org/c%2B%2B/clauseC.html
- deleted 11y ago[deleted]
- vortico 11y agoHowever, with all the "great" features of C++ like templates, RAII, and overloading, programmers use and abuse every corner of them to build overly complicated ways to basically create domain-specific languages. It happens in over half of the C++ libraries I've used, otherwise they would just make their API in C.
- GFK_of_xmaspast 11y agoThis is like going back to y2k and saying 'well if you want double-slash comments you should use the version of c that already has it, namely c++98, and it was a mistake to add it to c'
- kazinator 11y agoThe only way you can use double slash comments without a C version that already has them is to preprocess your code with some text filter (thereby creating your own dialect which has them). In the 1990's, dialects of C that had // comments were C++, various dialects accepted by C compilers having // comments as a nonconforming extension over C90, and C compilers supporting draft ISO C features. Changing dialects just for the sake of comments is silly; the right choice will usually be "don't use those comments". In C programming, I still don't use // comments today, to keep my code strictly compatible with C90 (in my opinion, the best C dialect to date). I have been a C++ developer for many years and of course used // comments; I have nothing against them, and in fact wouldn't design a language syntax that doesn't have a "from here to the end of the line" comment feature. (Text filtering to obtain C features is not unheard of, by the way. The CLISP sources use something called "varbrace": a text processor which provides mixed declarations and statements.)