Is C subset of C++ ?

Egy rovid peldat lathatunk arra, hogy C++ -nak nem valodi reszhalmaza a C.
Mivel ez a kerdes tobbszor felmerult itt, alljon itt egy pelda.

stst.c:


int main()
{
 struct valami {int attrib;} record = {.attrib=0};
 return 0;
}

$ ln -s stst.c stst2.cpp
$ make stst
cc stst.c -o stst
$ echo $?
0
$ make stst2
g++ stst2.cpp -o stst2
stst2.cpp: In function ‘int main()’:
stst2.cpp:3: error: expected primary-expression before ‘.’ token
make: *** [stst2] Error 1

Hozzászólások

Na, ezert nem lehet kernelt irni C++-ban :)

Amit nem lehet megirni assemblyben, azt nem lehet megirni.

Nem, nem resze. Se nem valodi, se nem nem valodi reszhalmaza. :)
==
`Have some wine,' the March Hare said in an encouraging tone.
Alice looked all round the table, but there was nothing on it but tea.

Bocs, de C-ben mi a bánat akar lenni ez a .attrib így, ahogy szerepel?

> mi a bánat akar lenni ez a .attrib

http://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Designated-Inits.html#Desig…

"In a structure initializer, specify the name of a field to initialize with `.fieldname =' before the element value."

ISO/IEC 9899:TC2 ( http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf )

125. oldal: "If a designator has the form '. identifier' then the current object (defined below) shall have structure or union type and the identifier shall be the name of a member of that type."

Ettől függetlenül a C90 nem támogatja:

$ gcc -W -ansi stst.c -o stst-ansi -pedantic
stst.c: In function 'main':
stst.c:3: warning: ISO C90 forbids specifying subobject to initialize

Már eleve a long long sincs a jelenlegi C++ szabványban, nem beszélve a többi C99 featureról, így triviális a címben szereplő állítás.

Ha meg olyat akarsz, ami a C90-ben fordul, de a C++-ban nem, tessék:

#include <stdlib.h>
int main()
{
  int* a = malloc(sizeof *a);
  free(a);
  return 0;
}

Ekkor:

$ gcc -W -ansi stst.c -o stst-ansi -pedantic
$ g++ stst2.cpp
stst2.cpp: In function 'int main()':
stst2.cpp:4: error: invalid conversion from 'void*' to 'int*'