c++ - Possible ambiguity with extern "C", overloading, and function pointers -


With normal tasks, any

  extern "C" int frotz (int) can write ; // In a header int frottage (int x) {return x; }   

With Function Pointer, however, it has been implemented incompatible with the compiler.

  extern "C" int Klutz (int (*) (int), int); Intel Clutz (int (* FTP) (int), return (* (FTP) (x); }   

In the announcement, the argument is also extern "c" . In most definitions, most compilers match these functions and make Klutz a extern "C" function. However, the Sun and the Cray Compiler interpret these functions as separation, create overloaded int Klutz (int (* fptr) (int), int x), which later Generate a link-time error

However, Section 7.5.5 of C ++ 98 and C ++ 11 guarantees interpretation of frotz , I am not Can tell whether the standard is unclear about whether to check for extern "C" matching overloading Take or should be.

Top Klutz a mangled (C ++) symbol or an extern "C" / code> symbol?

1 Edit

I can type to separate the function pointer for C or C + + AB, but I'm interested in what the code is here (A) Klutz defines that C + + is a linkage, (B) defines it to link C, or (C) is obscure according to the standard, so that the compiler can choose Are free to understand how to do it.

Edit 2

This may be a known issue, at least those searchable bug trace ckers by those compilers. In my tests, GCC, Clag, Intel, MSVC, IBM XL, Pathcases, PGI and Open 64 fail to separate all the function types which are similar to language linkage, as clearly as required by the standard (See section 7.5.1) Accepted answers) By deciding this, many existing codes will be broken and ABI changes will be required. I do not know about any compiler which actually uses a different calling configuration for the C vs. C ++ language linkage.

  • : "Finding the reason for asking to remove this feature from the next is standard relevant ;-)" ... "and we can also decide an official WantFix. "

  • :" I am really afraid of implementing this rule because it is to properly make the canonical type of language linkage, which is a toner code breaker "

    C ABI and C ++ ABI are the same The ass Not a problem. Therefore, a extern "C" function pointer is a different type than a C ++ function pointer. You need something like this:

      extern "c" {typedef int (* KlutzFuncType) (int); Intel Clutz (Klutzfunk type, int); } Intel Clutz (Klutzfanks Type FTPR, Int X) {Return (* FTPTR) (x); }   

    There is some discussion on this issue.


    I have only one copy. From 7.5p1:

    There are two different types of function types with different language relations, even if they are otherwise identical.

    My reading of this is that the parameters of your first Klutz are different from the first parameter of your second Klutz , And so should be your second Klutz C ++ link.


    There is a C ++ implementation that does not take into account the type of language, despite what the standard is called. In the following code snippet, KlutzCxxFuncType refers to the C ++ linkage with a function, whereas KlutzCFuncType refers to the function with the linkage.

      typedef int (* KlutzCxxFuncType) (int); Extern "C" {typedef int (* KlutzCFuncType) (integer); Intel Clutz (Clutzicfunk type, int); } Intel Clutz (Klutzcxfunk type PPTR, int x) {return (* FTPTR) (x); } Intel Clutz (KlutzCFuncType fptr, int x) {returns (* fptr) (x); }   

    A compiler separating the function types based on the language linkage will generate a redefinition error on this code. For example, G ++ 4.7.2 will emit :

      prog.cpp: "int klutz (KlutzCFuncType, int) in a function ????: Prog.cpp: 9: 5: Error: Redefine "Quatus" (KlutzCFuncType, int) ??? Prog.cpp: 8: 5: Error: Solitude Intel Clutz (Klutzicxfunk type, int) a ???? Defined    

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -