67 points · by bemmu · 11y ago · 54 comments
#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.h...
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.
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.
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. 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.
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.
To be fair, in C++ you have to say which overload you want to point a function pointer at. So the fact that you have to specify the real function you want to point at, in C, doesn't seem unreasonable.
// Well, you do say it ... with type!
int (*ptr)(int, int) = add;
This will take the address of add(int, int) and not some other add like add(double, double). There is no explicit way to refer to one or the other.You're right in that we can't point to the collection of add functions as such. There is no generic function add of which the overloads are its implementations, such that we can have a pointer to this generic add aggregate and have ptr(a, b) be subject to overload resolution.
I think that if you want to be able to construct an object ptr out of a set of plain functions, such that ptr(a, b) chooses among those functions based on arguments, that may be doable. Just not with a plain function pointer.
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.
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.)
Counterargument that destroys your entire premise: C's revisions( note that revisions are not flavors or separate languages ) don't fork.
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.
https://gustedt.wordpress.com/2015/05/11/the-controlling-exp...
http://stackoverflow.com/questions/479207/function-overloadi...
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).
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.
(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
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).
cc -std=c++14
No preprocessor necessary! Also it checks the type of both arguments.