As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center提供指导。
已关闭8年。
例如,如果我有以下功能:
内联代码使用
编译(使用
无条件调用
已关闭8年。
例如,如果我有以下功能:
void foo(DoThisSometimes, DoThisAlways)
{
if (DoThisSometimes == 1)
{
//Do stuff
}
//Do other stuff
{
内联代码使用
DoThisSometimes
为0调用此函数,是否有任何编译器会从内联函数中删除这部分代码:if (DoThisSometimes == 1)
{
//Do stuff
}
最佳答案
不错的编译器当然应该做到这一点,而GCC则应该这样做。以下来源:
#include <cstdio>
inline void foo(bool maybe)
{
if (maybe) {
printf("Maybe\n");
}
printf("Always\n");
}
int main()
{
foo(true);
foo(false);
}
编译(使用
-O3
优化)为:0000000000400410 <main>:
400410: 48 83 ec 08 sub $0x8,%rsp
400414: bf e4 05 40 00 mov $0x4005e4,%edi
400419: e8 d2 ff ff ff callq 4003f0 <puts@plt>
40041e: bf ea 05 40 00 mov $0x4005ea,%edi
400423: e8 c8 ff ff ff callq 4003f0 <puts@plt>
400428: bf ea 05 40 00 mov $0x4005ea,%edi
40042d: e8 be ff ff ff callq 4003f0 <puts@plt>
400432: 31 c0 xor %eax,%eax
400434: 48 83 c4 08 add $0x8,%rsp
400438: c3 retq
400439: 0f 1f 00 nopl (%rax)
无条件调用
puts
3次。关于c++ - 是否有任何C++编译器删除内联时始终保持相同答案的if语句? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14965171/