在osx/win7/linux上不同的c++调用转换

Different c++ calling conversion on osx/win7/linux

本文关键字:c++ 调用 转换 osx win7 linux      更新时间:2023-10-16

我遇到一个奇怪的问题。c++的调用约定似乎不同于win/linux和macosx

以下代码的行为在MacOSX/linux/Win7 (64bit)上不同

MacOSX: Apple LLVM version 6.1.0 (clang-602.0.53)(基于LLVM 3.6.0svn)

Linux: gcc version 4.8.4

Win7: VS2015

在Win7/Linux

:

this is method1
this is method2
this is method3
this is method4
this is method5
this is method6
this is method7
this is method8
this is method9
在MacOsx

this is method9
this is method8
this is method7
this is method6
this is method5
this is method4
this is method3
this is method2
this is method1

代码如下:

#include <iostream>
int   method1(){
    std::cout << "this is method1" << std::endl ;
    return 1;
}
int   method2(){
    std::cout << "this is method2" << std::endl;
    return 1;
}
int   method3(){
    std::cout << "this is method3" << std::endl;
    return 1;
}
int   method4(){
    std::cout << "this is method4" << std::endl;
    return 1;
}
int   method5(){
    std::cout << "this is method5" << std::endl;
    return 1;
}
int   method6(){
    std::cout << "this is method6" << std::endl;
    return 1;
}
int   method7(){
    std::cout << "this is method7" << std::endl;
    return 1;
}
int   method8(){
    std::cout << "this is method8" << std::endl;
    return 1;
}
int   method9(){
    std::cout << "this is method9" << std::endl;
    return 1;
}

void   caller(int, int,int, int,int, int,int, int,int){
}
int main(){
    caller(method1(), method2(),method3(), method4(),method5(), method6(),method7(), method8(),method9());
    return 0;
}

虽然调用约定确实因平台而异,但这不是调用约定不同的结果,而是求值顺序,这是实现定义的。
这意味着顺序可能不仅因操作系统而异,而且因编译器而异(理论上甚至在不同的编译器版本之间)。因此,您的代码不应该依赖于特定的求值顺序。