哪些平台对C和c++有不兼容的abi ?
What platforms have incompatible ABIs for C and C++?
我刚刚注意到c++标准规定C和c++函数具有不同且不兼容的类型,即使它们的类型签名相同(更多信息请参阅这个问题)。这意味着技术上不允许将c++函数传递给像pthread_create()
这样的C函数。
我很好奇在使用的平台中是否有两个abi实际上是不同的(除了明显的名称混淆差异)。具体来说,有没有人知道这个c++程序在哪些平台上无法编译和运行?
#include <assert.h>
extern "C" int run(int (*f)(int), int x) { return f(x); }
int times2(int x) { return x * 2; }
int main(int argc, char *argv[]) {
int a = times2(argc);
// This is undefined behavior according to C++ because I am passing an
// "extern C++" function pointer to an "extern C" function.
int b = run(×2, argc);
assert(a == b);
return a;
}
我不知道有哪个平台的ABI不同,但即使C和c++调用约定相同,c++标准也要求编译器为程序发出诊断。带有c语言链接的指向函数的指针与带有c++语言链接的指向函数的指针是不同的类型,因此您应该能够像这样重载run()
:
extern "C" int run(int (*f)(int), int x) { return f(x); }
extern "C++" int run(int (*f)(int), int x) { return f(x); }
现在,当你调用run(times)
时,它应该调用第二个,因此,第一个是不可调用的(没有从c语言链接的指针到函数到c++语言链接的指针到函数的转换),因此原始代码应该引起编译器诊断。但是,大多数编译器会出错,例如http://gcc.gnu.org/bugzilla/show_bug.cgi?id=2316
注意:Solaris编译器诊断不兼容的语言链接,作为警告:
"t.c", line 11: Warning (Anachronism): Formal argument f of type extern "C" int(*)(int) in call to run(extern "C" int(*)(int), int) is being passed int(*)(int).
如果用extern "C++"
函数重载run
,它会正确地为run(times)
调用extern "C++"
函数。
相关文章:
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 字符类型转换不兼容
- Qt:如何使不兼容的发送方/接收方参数兼容?
- 视觉工作室 2017;启用 /permissive 时,类型 "const wchar_t *" 的参数与类型 "PWSTR" 的参数不兼容
- 使用不兼容的分配器复制分配无序列图
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 该对象具有与成员函数不兼容的类型限定符.为什么会出现此错误?
- 我正在尝试将表的地址传递给要在另一个函数中使用的指针,但得到不兼容的指针类型
- 为什么范围算法与 std 的迭代器不兼容?
- Winpcap Findalldevs const char * 与 char * 不兼容
- C++ 类型的参数与 void (__cdecl*)(void) 类型的参数不兼容,当调用 std::atexit()
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 如何在C++中停止调用不兼容的方法?
- OPENCL 警告:不兼容的指针类型将'float __global[16]'传递给类型为 '__global float4 的参数 *
- 函数范围的静态变量如何导致与共享库中函数代码的未来使用不兼容
- 如何修复"方法的类型与 PInvoke 不兼容"
- C++不兼容的迭代器类型
- 升级到G 4.7(带有C 11支持):任何ABI不兼容
- boost 是否使用 C++11 "inline namespaces" 来避免运行时出现 ABI 不兼容错误?
- 哪些平台对C和c++有不兼容的abi ?