This is prompted by a an answer I gave to a current question which asks about a generics library for C - the questioner specifically states that they do not want to use C++.
C is a complete programming language. C is not an arbitrary subset of C++. C is not a subset of C++ at all.
This is valid C:
foo_t* foo = malloc ( sizeof(foo_t) );
To make it compile as C++ you have to write:
foo_t* foo = static_cast<foo_t*>( malloc ( sizeof(foo_t) ) );
which isn't valid C any more. (you could use the C-style cast, it which case it would compile in C, but be shunned by most C++ coding standards).
They are not the same language, and if you have an existing project in C you don't want to rewrite it in a different language just to use a library. You would prefer to use libraries which you can interface to in the language you are working in.
Taking the first C file in a project I'm working on, this is what happens if you just swap gcc std=c99
for g++
:
sandiego:$ g++ -g -O1 -pedantic -mfpmath=sse -DUSE_SSE2 -DUSE_XMM3 -I src/core -L /usr/lib -DARCH=elf64 -D_BSD_SOURCE -DPOSIX -D_ISOC99_SOURCE -D_POSIX_C_SOURCE=200112L -Wall -Wextra -Wwrite-strings -Wredundant-decls -Werror -Isrc src/core/kin_object.c -c -o obj/kin_object.o | wc -l
In file included from src/core/kin_object.c:22:
src/core/kin_object.h:791:28: error: anonymous variadic macros were introduced in C99
In file included from src/core/kin_object.c:26:
src/core/kin_log.h:42:42: error: anonymous variadic macros were introduced in C99
src/core/kin_log.h:94:29: error: anonymous variadic macros were introduced in C99
...
cc1plus: warnings being treated as errors
src/core/kin_object.c:101: error: ISO C++ does not support the ‘z’ printf length modifier
..
src/core/kin_object.c:160: error: invalid conversion from ‘void*’ to ‘kin_object_t*’
..
src/core/kin_object.c:227: error: unused parameter ‘restrict’
..
src/core/kin_object.c:271: error: ISO C++ does not support the ‘z’ printf length modifier
src/core/kin_object.c:271: error: ISO C++ does not support the ‘z’ printf length modifier
In total 69 lines of errors, four of which are invalid conversions, but mostly for features that exist in C99 but not in C++.
It's not like I'm using those features for the fun of it. It would take significant work to port it to a different language.
So it is plain wrong to suggest that
[a] C compiler is almost certainly really a C++ compiler, so there are no software cost implications
There are often significant cost implications in porting existing C code to the procedural subset of C++.
So suggesting 'use the C++ std::queue class' as an answer to question looking for an library implementation of a queue in C is dafter than suggesting 'use objective C' and 'call the Java java.util.Queue class using JNI' or 'call the CPython library' - AFAIK Objective C actually is a proper superset of C (though not C99), and Java and CPython libraries both are callable directly from C without having to port unrelated code to the C++ language.
Of course you could supply a C façade to the C++ library, but once you're doing that C++ is no different to Java or Python.