When should I use [[maybe_unused]]?

后端 未结 2 1777
情话喂你
情话喂你 2020-12-15 03:31

What is good about using [[maybe_unused]]? Consider

int winmain(int instance, int /*prevInstance*/, const char */*cmdline*/, int show);

int win         


        
2条回答
  •  被撕碎了的回忆
    2020-12-15 04:14

    Baum mit Augen's answer is the definitive and undisputed explanation. I just want to present another example, which doesn't require macros. Specifically, C++17 introduced the constexpr if construct. So you may see template code like this (bar the stupid functionality):

    #include 
    
    template
    auto add_or_double(T t1, T t2) noexcept {
        if constexpr (std::is_same_v)
            return t1 + t2;
        else
            return t1 * 2.0;
    }
    
    int main(){
        add_or_double(1, 2);
        add_or_double(1.0, 2.0);
    }
    

    As of writing this, GCC 8.0.1 warns me about t2 being unused when the else branch is the instantiated one. The attribute is indispensable in a case like this too.

提交回复
热议问题