Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The C ABI and the C++ ABI are not guaranteed to be the same. So, an <code>extern "C"</code> function pointer is a different type from a C++ function pointer. You need something like this:</p> <pre><code>extern "C" { typedef int (*KlutzFuncType)(int); int Klutz (KlutzFuncType, int); } int Klutz (KlutzFuncType fptr, int x) { return (*fptr)(x); } </code></pre> <p>There is some discussion of this issue <a href="https://stackoverflow.com/q/11873939/315052">here</a>.</p> <hr> <p>I only have a copy of the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf" rel="nofollow noreferrer">draft</a>. From 7.5p1:</p> <blockquote> <p>Two function types with different language linkages are distinct types even if they are otherwise identical.</p> </blockquote> <p>My reading of this is that the first parameter of your first <code>Klutz</code> has a different type than the first parameter of your second <code>Klutz</code>, and so your second <code>Klutz</code> should have C++ linkage.</p> <hr> <p>There are C++ implementations that do not take language linkage into account for function types, despite what the standard says. In the following code snippet, <code>KlutzCxxFuncType</code> refers to a function with C++ linkage, while <code>KlutzCFuncType</code> refers to a function with C linkage.</p> <pre><code>typedef int (*KlutzCxxFuncType)(int); extern "C" { typedef int (*KlutzCFuncType)(int); int Klutz (KlutzCFuncType, int); } int Klutz (KlutzCxxFuncType fptr, int x) { return (*fptr)(x); } int Klutz (KlutzCFuncType fptr, int x) { return (*fptr)(x); } </code></pre> <p>A compiler that does not distinguish function types based on language linkage will generate a redefinition error on this code. For example, <code>g++ 4.7.2</code> will emit:</p> <pre><code>prog.cpp: In function ‘int Klutz(KlutzCFuncType, int)’: prog.cpp:9:5: error: redefinition of ‘int Klutz(KlutzCFuncType, int)’ prog.cpp:8:5: error: ‘int Klutz(KlutzCxxFuncType, int)’ previously defined here </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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