Possible ambiguity with extern “C”, overloading, and function pointers

后端 未结 1 1642
旧巷少年郎
旧巷少年郎 2020-12-16 17:35

With normal functions, one can write

extern \"C\" int Frotz(int);  // in a header

int Frotz(int x) { return x; }

With function pointers, h

相关标签:
1条回答
  • 2020-12-16 18:05

    The C ABI and the C++ ABI are not guaranteed to be the same. So, an extern "C" function pointer is a different type from a C++ function pointer. You need something like this:

    extern "C" {
        typedef int (*KlutzFuncType)(int);
        int Klutz (KlutzFuncType, int);
    }
    
    int Klutz (KlutzFuncType fptr, int x) { return (*fptr)(x); }
    

    There is some discussion of this issue here.


    I only have a copy of the draft. From 7.5p1:

    Two function types with different language linkages are distinct types even if they are otherwise identical.

    My reading of this is that the first parameter of your first Klutz has a different type than the first parameter of your second Klutz, and so your second Klutz should have C++ linkage.


    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, KlutzCxxFuncType refers to a function with C++ linkage, while KlutzCFuncType refers to a function with C linkage.

    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); }
    

    A compiler that does not distinguish function types based on language linkage will generate a redefinition error on this code. For example, g++ 4.7.2 will emit:

    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
    
    0 讨论(0)
提交回复
热议问题