-1

Can someone explain this note in C++ primer 5th edition to me:

Note

The functions that C++ inherits from the C library are permitted to be defined as C functions but are not required to be C functions—it’s up to each C++ implementation to decide whether to implement the C library functions in C or C++.

rustyx
  • 80,671
  • 25
  • 200
  • 267
Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • 3
    Well, the functions that C++ inherits from the C library are permitted to be defined as C functions but are not required to be C functions. It’s up to each C++ implementation to decide whether to implement the C library functions in C or C++. – Sneftel Sep 05 '21 at 23:38
  • 1
    I am voting to reopen this question, as it is asking about intricate details of C++. It has enough information. It can only be answered by someone knowledgeable in the topic. – Robert Andrzejuk Sep 06 '21 at 07:05

2 Answers2

1

I will give you an example of one function, that could be "treated" as C or C++. std::toupper() can also be written as toupper(). I believe the first one uses some safety checks of C++, while the other one is strictly C. But what it boils down to is:

#include <cctype>
#include <iostream>

int main (void) {
    char c = 'b';

    std::cout << toupper(c);                                                                     

    return 0;
}

compiles using C way, while:

#include <cctype>
#include <iostream>

int main (void) {
    char c = 'b';

    std::cout << std::toupper(c);                                                                     

    return 0;
}

Uses C++ compilation addressing namespaces, ant that is what @numzero's answer talks about.

Now both will compile, but it is up to you and your own risk to use C function.

  • 1
    Your should be including proper headers to discuss it compiling or not. Otherwise both version are at your own risk. – StoryTeller - Unslander Monica Sep 06 '21 at 22:40
  • Um, where did you find *that* `toupper`? `std::toupper` is not a member function, it can’t use `this`, neither can it be `const`. That’s probably something from char_traits. – numzero Sep 06 '21 at 22:59
  • @numzero you were right, it was this https://gcc.gnu.org/onlinedocs/gcc-7.5.0/libstdc++/api/a00389_source.html –  Sep 06 '21 at 23:57
-2

There are functions like printf which are defined in the C standard library but are required to be usable in C++ as well. However, C and C++ differ in how they link modules together: in C, functions are matched by name only, but in C++ full signature is normally used¹ ². But to allow interoperability, it is possible (using extern "C") to force a C++ function to use C linkage so that it will match the identically-named C function (definition may be on either side).

What the quoted statement seems to state is that the functions that C++ inherits from the C standard library may be the actual C functions, but that is not required. They are also allowed to be defined as regular C++ functions (behaving equivalently, but not strictly identical, to the C counterparts) in C++.

¹ That is needed to support overloading; things return value type may not be used as it doesn’t participate in overload resolution.

² There might be some other differences as well.

numzero
  • 2,009
  • 6
  • 6