Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio equivelent of gcc __attribute__((unused)) in c++11 (or lower)?

I am trying to write a macro to use suppress unused variable warnings when the user wants them (e.g. in derived classes when you have not implemented the whole class yet). I know that I can remove the variable name... but to make it clear I would prefer a macro).

So far I have this:

#ifdef WIN32
    #define UNUSED(x) x
#else
    #define x __attribute__((unused))
#endif

Used like:

void test_fn(int UNUSED(test_var)) {...}

I saw this post: suppressing-is-never-used-and-is-never-assigned-to-warnings-in-c-sharp, but it gave me a result that I can't really use (multiline #pragmas).

So my question is, is there a MSVS equivalent of the __attribute__((unused))? - i.e. on the same line?

Note: this question does not answer how to do what I am asking: how-do-i-best-silence-a-warning-about-unused-variables since it does not cover how to use it within the function prototype in a way that works with both MSVS and gcc.

like image 505
code_fodder Avatar asked Mar 06 '23 13:03

code_fodder


1 Answers

If a variable or function-argument is potentially unused, gcc's __attribute__((unused)) is designed to suppress any warning about it.

Now, if you want something portable, there are multiple choices:

  1. If you don't use it,
    1. and it's a function-argument, just don't name it.
    2. otherwise, simply don't create it.
  2. If it might be used under some circumstances, simply use it once definitely by casting to void:

    (void)potentially_unused;
    

    Yes, the second option is not in the prototype, but one has to make allowances.

  3. Upgrade to C++17 and use [[maybe_unused]].

like image 80
Deduplicator Avatar answered Apr 06 '23 15:04

Deduplicator