Which parts of the C standard library are not covered by (the rest of) the C++ standard library?

StackOverflow https://stackoverflow.com/questions/22269370

  •  11-06-2023
  •  | 
  •  

Pregunta

The C++ library includes the same definitions as the C language library

But the C++ library seems to duplicate (/extend) some of the functionality of the C library in non-C-library headers. For example, the C library has <string.h>, and the C++ library has both <cstring> and <string>; the C library has <time.h>, and the C+ library has both <ctime> and <chrono>.

If I need a string class, I assume that I am better off using <string> instead of <cstring>, because <string> can benefit from all the non-C functionality in C++ (e.g. exceptions). But there is functionality in the C library that doesn't exist in any other form in the C++ library. For example, I couldn't find anything like memcpy and memcmp outside <cstring>.

Which parts of the C library have no analogue in the non-C-library headers?

(If the version of the C++ standard matters for this, I am interested in C++11.)

¿Fue útil?

Solución

There aren't that many headers, so lets just list them. Some could be replaced by language facilities, rather than libraries. (I haven't enumerated every function in each header, so I might have missed the odd function that doesn't have a C++ alternative when most of its colleagues do.)

C library         C++ alternatives
assert.h          Exceptions
complex.h         <complex>
ctype.h           None (or maybe <locale>, if you want to jump down that rabbit-hole)
errno.h           None (but only applies to C functions)
fenv.h            None
float.h           <limits>
inttypes.h        (See breakdown)
   formatting     <iostream>
   strto...       <string> (C++11), <sstream>
   imaxabs        std::abs overloads
   imaxdiv        std::div overloads
iso646.h          Language
locale.h          <locale>
math.h            None (extended with C++ overloads)
setjmp.h          Exceptions
signal.h          None
stdarg.h          Variadic templates (C++11)
stdbool.h         Language
stddef.h          None
stdint.h          None
stdio.h           <iostream> etc.
stdlib.h          (See breakdown)
   atof etc.      <sstream>, <string> (C++11)
   rand etc.      <random> (C++11)
   malloc etc.    new, containers
   abort etc.     None
   bsearch etc.   <algorithm>
   abs etc.       None (extended with C++ overloads)
   mb strings     None
string.h          <string>, <algorithm>
tgmath.h          <cmath> (C++ overloads)
time.h            <chrono> (C++11)
wchar.h           <iostream> etc.
wctype.h          None

To summarise:

Which parts of the C library have no analogue in the non-C-library headers?

[w]ctype.h, errno.h, fenv.h, fenv.h, math.h, signal.h, stddef.h, stdint.h, some of stdlib.h. Before C++11, also stdarg.h, time.h and more of stdlib.h

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top