[英]Can C++ Compilers optimize overloaded default parameters?
I've been refactoring a codebase for an embedded chip that uses a lot of convenient overloaded functions with default parameters, like this:我一直在重构一个嵌入式芯片的代码库,它使用许多带有默认参数的方便的重载函数,如下所示:
int state, prevState;
float time;
void setState(int newState, float newtime)
{
if (newState != state)
{
prevState = state;
time = newTime;
}
state = newState;
}
inline void setState(int newState)
{
setState(newState, time);
}
In its current implementation, the second function is manually optimized:在当前的实现中,第二个 function 是手动优化的:
void setState(int newState)
{
if (newState != state)
prevState = state;
state = newState;
}
If I use the new implementation (the one with inline) is there a way for the compiler to recognize and remove the code involving time, or is the old manual way the best practice?如果我使用新的实现(带有内联的实现),编译器是否有办法识别和删除涉及时间的代码,或者旧的手动方式是最佳实践吗?
I've used Godbolt's compiler on GCC yet can't find an appropriate setting for code or a compile flag that doesn't obfuscate everything, or have the calls remain.我在 GCC 上使用了 Godbolt 的编译器,但找不到合适的代码设置或不会混淆所有内容或保留调用的编译标志。
GCC generates exactly the same code for both variants with -O3
: godbolt . GCC 为带有
-O3
的两个变体生成完全相同的代码: godbolt 。
Note the the inline
keyword is at best only considered as a suggestion in the compiler's decision regarding inlining.请注意,
inline
关键字充其量仅被视为编译器关于内联的决定中的建议。 It's main effect is something completely different .它的主要作用是完全不同的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.