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

后端 未结 4 1698
天命终不由人
天命终不由人 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:36

    Cast it to void:

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

    This basically tells the compiler "Yes I know I'm discarding this, yes I'm sure of it."

提交回复
热议问题