How can I intentionally discard a [[nodiscard]] return value?

后端 未结 4 1704
天命终不由人
天命终不由人 2020-12-18 18:07

Say I have

[[nodiscard]] int foo ()
{
    return 0;
}

int main ()
{
    foo ();
}

then

error: ignoring return value of ‘in         


        
4条回答
  •  没有蜡笔的小新
    2020-12-18 18:28

    You can also tag the returned int with another tag:

    [[nodiscard]] int foo ()
    {
        return 0;
    }
    
    int main ()
    {
        [[maybe_unused]] int i = foo ();
    }
    

    Could be useful if you have some debug-only code that requires the value.

提交回复
热议问题