Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the functional difference between "(void) cast" vs. "__attributes__" for silencing unused argument warnings? [duplicate]

Is there any functional difference between marking a virtual method's unused parameter arguments with GCC's __attribute__((unused)) and casting the argument to (void)?

class Other {
    virtual int sum(int a, int b, int c);
};

class Example : Other {
    virtual int sum(int a, int b, int c __attribute__((unused))) override {
        return a + b;
    }
};

class Example2 : Other {
    virtual int sum(int a, int b, int c) override {
        (void)c;
        return a + b;
    }
};

Both do the job of silencing unused argument warnings and neither of them warn, if the variable is used later. Though the GCC __attribute__ is longer.

like image 866
ktb Avatar asked Nov 17 '25 11:11

ktb


1 Answers

There is no functional difference, as both may result in no-operation. (void) casting has an upper-hand as it works in all the platforms.

Another approach is to use a macro to silence such arguments. So when you disable the macro, you will know all the unused parameters in your code.

#define UNUSED(X) (void)X 

However in this specific scenario, I would prefer simply not to mention the unused argument. It assures the future reader of the code that the unmentioned argument is guaranteed to be not in use.

int sum(int a, int b, int /*c*/) override {  // comment is optional
  return a + b;
}
like image 192
iammilind Avatar answered Nov 19 '25 01:11

iammilind



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!