小编Yux*_*xai的帖子

C++ 代码在不同的优化标志下表现不同

有四个文件,header.hA.cppB.cppmain.cpp

// header.h
struct test {
  static inline int get();
};
Run Code Online (Sandbox Code Playgroud)
// a.cpp
#include "header.h"
int testA() { return test::get(); }
int test::get() { return 0; }
Run Code Online (Sandbox Code Playgroud)
// b.cpp
#include "header.h"
int testB() { return test::get(); }
int test::get() { return 1; }
Run Code Online (Sandbox Code Playgroud)
// main.cpp
#include <cstdio>
int testA();
int testB();

int main() {
  printf("%d %d\n", testA(), testB());
}
Run Code Online (Sandbox Code Playgroud)

编译时g++ -o main A.cpp B.cpp main.cpp -O0二进制输出两个相同的数字,而-O3始终输出0 1 …

c++ language-lawyer

0
推荐指数
1
解决办法
94
查看次数

标签 统计

c++ ×1

language-lawyer ×1