我尝试运行一个简单的 C++ 程序,但我不确定为什么不同的编译器提供不同的输出
#include <iostream>
struct A
{
A() { std::cout << "Object A created" << std::endl; }
friend std::ostream& operator<<(std::ostream& os, const A& a)
{
os << a.str << "\n";
return os;
}
static bool printMe()
{
std::cout << "static bool printMe() of object A" << std::endl;
return true;
}
std::string str{"This is object A"};
};
int main()
{
std::cout << A() << (A::printMe() ? "TRUE" : "FALSE") << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
OUTPUT1:一些编译器提供以下输出:
Object A created
This is object A
static bool printMe() of object A
TRUE
Run Code Online (Sandbox Code Playgroud)
例如: https: //www.programiz.com/cpp-programming/online-compiler/
OUTPUT2:其他编译器提供其他输出:
static bool printMe() of object A
Object A created
This is object A
TRUE
Run Code Online (Sandbox Code Playgroud)
例如: http: //cpp.sh/
我无法理解为什么有些编译器在创建对象 A 之前执行静态函数,我希望在调用静态函数之前保留顺序并创建对象 A,如 OUTPUT2 中所示。
在 C++17 之前,表达式中子表达式的求值顺序大多是未指定的,编译器可以按照自己认为合适的方式自由地对它们进行排序。在 C++17 中,顺序的定义更加严格,特别是:
在移位运算符表达式
E1<<E2and中E1>>E2,E1 的每个值计算和副作用都在 E2 的每个值计算和副作用之前排序[cppreference]
因此,如果添加-std=c++17,您应该只能看到 OUTPUT1。
?的顺序总是被定义的,所以这是安全的。