6 ms·
Can you provide C code that will not compile with c++ compiler?
by samfisher83 10y ago
Can you provide C code that will not compile with c++ compiler?
- msoucy 10y agoSure: struct Foo { int virtual; };
- metaobject 10y agoYou're being pedantic about reserved words?
- spc476 10y agoI had that same problem. I wrote a C library [1] that has a structure field named "class". It's "class" because the protocol being described (DNS) calls that particular field "class" [2]. And I'm not about to pay lip service to an abomination like C++ [3][4]. [1] https://github.com/spc476/SPCDNS https://github.com/spc476/SPCDNS [2] https://github.com/spc476/SPCDNS/blob/ca5052c3d0c3252071a18ebbada808a7b536c860/src/dns.h#L255 https://github.com/spc476/SPCDNS/blob/ca5052c3d0c3252071a18e... [3] I am NOT a fan of C++. [4] But I had to anyway, but I used the C pre-processor to rename the field.
- camgunz 10y agoYeah I super hate this. There's no other language that requires other languages to contort themselves like C++ does. Every time I see `#ifdef __cplusplus` or `klass` my blood pressure spikes.
- astrodust 10y agoHere's a bunch of examples: http://www.geeksforgeeks.org/write-c-program-wont-compiler-c/ http://www.geeksforgeeks.org/write-c-program-wont-compiler-c...
- Chromozon 10y agoThis is a great example of how many of the things in C that don't compile in C++ are horrible programming practices, and it's really nice that C++ doesn't allow such garbage.
- magnusjonsson 10y agoActually those are very bad examples. In practice, compilers will produce warnings in all of those examples. Better examples (of actually useful things) would be things like designated initializers, struct literals, declaring array lengths in args using static, etc.
- milesrout 10y agoI don't see what is 'horrible programming practice' about int *x = malloc(sizeof(int));
- stephencanon 10y agoBesides the various "bad old programming idioms" that do not compile under C++, there are a few genuinely useful features that have only just been added to C++ more than 15 years after they appeared in C, like hexadecimal floating-point literals (in practice, many compilers supported these, but they were not formally part of the language). Designated struct initializers are another C feature that I would love to have in C++.
- uzoodoo 10y ago#include <stdlib.h> int main() { int *x = malloc(sizeof(int)); return 0; }
- Ace17 10y agoint* p = malloc(256);
- hawski 10y agoCompound literals are outside of C++ standard. However GCC for example supports them, but a bit differently [0]. $ cat foobar.c #include <stdio.h> struct foo { int x; int y; }; int foobar(struct foo *f) { return f->x + f->y; } int main(int argc, char *argv[]) { (void)argc; (void)argv; printf("%d\n", foobar(&(struct foo){10, 20})); return 0; } $ gcc -Wall -Wcast-align -Wextra -pedantic -std=c99 foobar.c -o foobar && ./foobar 30 $ g++ -Wall -Wcast-align -Wextra -pedantic foobar.c -o foobar && ./foobar foobar.c: In function ‘int main(int, char**)’: foobar.c:17: warning: ISO C++ forbids compound-literals foobar.c:17: warning: taking address of temporary 30 $ # taking address of temporary means that it's undefined behavior [0] https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html
- sampo 10y ago> Can you provide C code that will not compile with c++ compiler? C99 native complex numbers