Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>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++.</p> </blockquote> <p>C is a complete programming language. C is not an arbitrary subset of C++. C is not a subset of C++ at all. </p> <p>This is valid C:</p> <pre><code>foo_t* foo = malloc ( sizeof(foo_t) ); </code></pre> <p>To make it compile as C++ you have to write:</p> <pre><code>foo_t* foo = static_cast&lt;foo_t*&gt;( malloc ( sizeof(foo_t) ) ); </code></pre> <p>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, and also by many C programmers; witness the "don't cast malloc" comments all over Stack Overflow).</p> <hr> <p>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. (In some cases this is possible with a few <code>extern "C"</code> wrapper functions, depending on how template/inline a C++ library is.)</p> <p>Taking the first C file in a project I'm working on, this is what happens if you just swap <code>gcc std=c99</code> for <code>g++</code>:</p> <pre><code>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 </code></pre> <p>In total 69 lines of errors, four of which are invalid conversions, but mostly for features that exist in C99 but not in C++.</p> <p>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.</p> <p>So it is plain wrong to suggest that </p> <blockquote> <p>[a] C compiler is almost certainly really a C++ compiler, so there are no software cost implications </p> </blockquote> <p>There are often significant cost implications in porting existing C code to the procedural subset of C++.</p> <p>So suggesting <em>'use the C++ std::queue class'</em> as an answer to question looking for an library implementation of a queue in C is dafter than suggesting <em>'use objective C'</em> and <em>'call the Java java.util.Queue class using JNI'</em> or <em>'call the CPython library'</em> - Objective C actually is a proper superset of C (including C99), and Java and CPython libraries both are callable directly from C without having to port unrelated code to the C++ language. </p> <p>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. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload