Mike Manilone
Mike Manilone

Reputation: 590

In C++, what's the difference between a "C" function and a "C" linkage?

As is stated by the standard, language linkage is part of the function type, and cppreference gives the following examples

extern "C" void f1(void(*pf)()); // declares a function f1 with C linkage,
                             // which returns void and takes a pointer to a C function
                             // which returns void and takes no parameters
extern "C" typedef void FUNC(); // declares FUNC as a C function type that returns void
                                // and takes no parameters
FUNC f2;            // the name f2 has C++ linkage, but its type is C function
extern "C" FUNC f3; // the name f3 has C linkage and its type is C function void()
void (*pf2)(FUNC*); // the name pf2 has C++ linkage, and its type is
                    // "pointer to a C++ function which returns void and takes one
                    // argument of type 'pointer to the C function which returns void
                    // and takes no parameters'"
extern "C" {
    static void f4();  // the name of the function f4 has internal linkage (no language)
                      // but the function's type has C language linkage
}

I'm really confused by C function type and C linkage. What is the difference between them, except mangling? What does a C function with a C++ linkage mean? Thanks!

UPDATE: this isn't asking what extern "C" does, since that doesn't answer why a C function can have a C++ linkage, and moreover, what makes a C function (inside C++), and why std::qsort, std::bsearch must have two overloads.

Upvotes: 3

Views: 86

Answers (2)

user743382
user743382

Reputation:

I'm really confused by C function type and C linkage. What is the difference between them, except mangling?

In standard C++, "C function" and "C++ function" are different types in much the same way that int and long are different types even if INT_MAX == LONG_MAX. It doesn't matter that in a lot of implementations the types behave exactly the same. What matters is that in some implementations, the types may behave differently, and all implementations should treat the types as distinct because of that. It means that in standard C++,

extern "C" void f();
void (*fp)() = &f; // ERROR

is invalid, because the initialiser is incompatible with the type being initialised. There is no implicit conversion between "pointer to C function" and "pointer to C++ function".

The most realistic hypothetical scenario of an implementation that cannot treat C and C++ functions as the same type is an implementation which uses different calling conventions for C vs. C++ functions.

What does a C function with a C++ linkage mean?

I'm guessing this is about

FUNC f2;            // the name f2 has C++ linkage, but its type is C function

It means the function is called in exactly the same way that a C function gets called, but the name gets mangled to include function parameter information etc. as usual, allowing it to be overloaded.

Upvotes: 1

M.M
M.M

Reputation: 141618

In the examples you quote, "C function" means "Function whose type has C language linkage".

Maybe you are not mentally separating the two different concepts:

  1. Language linkage of a function type.
  2. Language linkage of an identifier.

What does a C function with a C++ linkage mean?

I guess you are referring to FUNC f2; where the function type has C linkage and the identifier f2 has C++ linkage.

Upvotes: 2

Related Questions