考虑以下代码:
#include <iostream>
struct A {};
struct B {};
int func1(A *a, B *b, int c, double *d) {
int tmp = 0;
tmp = tmp;
return 1;
}
int func2(A *a, B *b, int c, double *d) {
return 1;
}
int main(int argc, char* argv[]) {
if (func1 == func2) {
std::cout << "equal" << std::endl;
} else {
std::cout << "not equal" << std::endl;
}
return 0;
}
在 VS2013 的 Release 配置中编译时,它会打印出“equal”。我有一个依赖于函数地址比较的库。你可以想象它在 Release 中不能正常工作。有没有办法在 VC++ 中防止这种优化?还是我应该提交错误?
最佳答案
这是 "feature" Microsoft 的链接器,并且文档警告您
Because /OPT:ICF can cause the same address to be assigned to different functions or read-only data members (const variables compiled by using /Gy), it can break a program that depends on unique addresses for functions or read-only data members.
您可以通过将 /opt:noicf
传递给链接器来关闭它。
关于c++ - 如何告诉 VC++ 优化器不要合并函数体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25495978/