简体   繁体   English

C++ 编译器可以优化重载的默认参数吗?

[英]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.

相关问题 C ++编译器可以优化一个类吗? - Can C++ compilers optimize away a class? C++ 编译器可以优化对 at() 的调用吗? - Can C++ compilers optimize calls to at()? C ++编译器可以优化“for”循环中的“if”语句吗? - Can C++ compilers optimize “if” statements inside “for” loops? C ++使用默认参数选择“错误”重载方法 - C++ chooses 'wrong' overloaded method with default parameters (C ++)构造函数,默认参数,“重载的调用……模棱两可” - (C++) Constructor, default parameters, “call of overloaded… ambigous” C ++编译器是否通过const引用POD参数优化传递? - Do C++ compilers optimize pass by const reference POD parameters into pass by copy? C++中重载函数和多个转换运算符的歧义,编译器不同意 - Overloaded function and multiple conversion operators ambiguity in C++, compilers disagree C&C ++编译器是否优化了与函数调用的比较? - Do C & C++ compilers optimize comparisons with function calls? 是否有任何 C 或 C++ 编译器在定义宏中进行优化? - Do any C or C++ compilers optimize within define macros? 编译器是否在c ++中优化静态值的if语句 - Do compilers optimize if-statements for static values in c++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM