[dcl.link]/4
:
最里面的一个决定了语言的联系。连杆规格
在命名空间范围中。在联动装置规范中,
指定的
、带有外部链接的函数名和变量
[示例:
extern "C" // the name f1 and its function type have C language linkage;
void f1(void(*pf)(int)); // pf is a pointer to a C function
结束示例]
观察指针
&foo
传递给函数
c_f()
不
指向C函数的指针。该代码在VS2017中正常编译和链接。但根据[dcl.link]/4,它不应该这样。
main.cpp
:
#include <stdio.h>
extern "C" // the name c_f and its function type have C language linkage;
void c_f(void(*pf)(int)); // pf is a pointer to a C function
void foo(int i) {
printf("%d\n", i);
}
extern "C" void c_foo(int);
int main() {
c_foo(1); // Calls c_foo(int) defined in other.c
c_f(&foo); // Calls c_f(void(*)(int)) defined in other.c, but &foo is not a pointer to a C function !!
}
文件
other.c
:
#include <stdio.h>
void c_f(void(*pf)(int)){
pf(2);
}
void c_foo(int i) {
printf("%d\n", i);
}
c_f()
和中的代码
main.cpp
1.
clang
也不在
GCC
1) 如果我们假设需要诊断