9 ms·
Function overloading in C
- deleted 11y ago[deleted]
- RoboSeldon 11y agoWorks with Clang and GCC. Obviously, Visual Studio doesn't work (they have almost complete support for C99 though, so maybe in a few years they will include C11 support).
- cremno 11y agoOnly library-wise: the reason for _Generic <tgmath.h> is missing, <complex.h> exists but can't be used. Some lesser used and sometimes useful C99 language features/changes are still unimplemented. Even excluding features C11 made optional (for good reasons) like VLA or complex.
- detaro 11y agoUnlikely, unless they need it for C++. Even C99 only gets better when they need parts of it for C++ anyways, and is not developed further.
- o87dv 11y agoI was expecting something interesting. Generic selection is built into the language: http://en.cppreference.com/w/c/language/generic http://en.cppreference.com/w/c/language/generic
- cremno 11y agoI recommend reading the notes section and better yet the following blog entry before one decides to use _Generic with less trivial types: https://gustedt.wordpress.com/2015/05/11/the-controlling-expression-of-_generic/ https://gustedt.wordpress.com/2015/05/11/the-controlling-exp...
- nautical 11y agoThis answer is more detailed .. http://stackoverflow.com/questions/479207/function-overloading-in-c/13010401#13010401 http://stackoverflow.com/questions/479207/function-overloadi...
- travjones 11y agoI don't program in C, but I can understand most of this code; However, I'm not sure where the "function overloading" part comes in or why it's significant. Could someone explain?
- judofyr 11y agoThis is a good explanation: https://www.reddit.com/r/programming/comments/3en2px/til_you_can_use_function_overloading_in_c/ctgj9zv https://www.reddit.com/r/programming/comments/3en2px/til_you...
- kazinator 11y agoThis 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.
- crystalgiver 11y agoInteresting approach, this is how I do it: cc -std=c++14 No preprocessor necessary! Also it checks the type of both arguments.
- ambrop7 11y agoIndeed. No need to use "complex" features like RAII/RTTI/exceptions. Generally, I see features like classes, overloading and templates as "compatible" with C-style coding.
- halosghost 11y agoThis is a very simple example. It only checks the type of the first parameter. _Generic can operate on all parameters, it just leads to an exponential increase of mess for each additional parameter handled. While I like _Generic in C11, there is no doubt that it is not as powerful or handy as Templates (this coming from someone who does not really enjoy Cxx templates).
- dllthomas 11y agoIt's hard to say what's more powerful. Certainly templates involve more computation, but _Generic can do some things templates can't (for good and for ill) and vice-versa.
- halosghost 11y agoWell, since templates are turing complete, I would tend to say that they are more powerful. But they work very differently. Templates do code generation and _Generic is just a fancy-pants type-based macro-dispatch. Like I said, I do not think _Generic is a bad thing (though it can get really messy—which is not to say that Templates can't), just that the example given in the OP is a bit contrived.
- dllthomas 11y agoIt is computationally more powerful, sure. If that's what you'd intended to be talking about, then fine. But that is a distinct notion from "you are able to do more with it" - which is typically the more relevant bit when discussing the power of a feature in a programming language outside a restricted academic context. (Incidentally, other things being equal - including ability to get done the thing you want to get done - "Turing complete" is a bug, not a feature!) Expanding a little - "Turing complete" means that your system is capable of computing anything that can be computed in some encoding, but that doesn't mean that it is linked to the rest of the world in a way that will do something useful with that encoding of the result. Token-manipulating C macros, while obviously of less powerful in the sense of computability, can do things that templates cannot do (name mangling, annotations, compiler pragmas) - and with _Generic, any of these can now be type directed. Of course, with great power comes the responsibility never to use half of that... but that's pretty true of C++ as well :p
- Animats 11y ago#define add(a, b) _Generic(a, int: addi, char*: adds)(a, b) Just because you can doesn't mean you should. Someday, someone will have to read that code. Note that when "add" returns a string, it's done an allocation, but for the integer case, it hasn't called "malloc". Now you'll now need to know the type to get rid of it. Back in C99, generic trig functions were put in to make the number-crunching people happy. After all, FORTRAN has them. Everybody was tired of different versions of math libraries for "float" and "double". Then in C11, the mechanism behind that, "_Generic", was exposed. Now, anybody can use it. But, probably, they shouldn't. [1] [1] http://www.robertgamble.net/2012/01/c11-generic-selections.html http://www.robertgamble.net/2012/01/c11-generic-selections.h...
- maxlybbert 11y ago> Just because you can doesn't mean you should. The fact that it's new doesn't mean it's in bad taste. GCC has had a similar mechanism in C for decades. It was experience with that mechanism, and similar extensions in other compilers, that led to it being in the C Standard.