How to clear unused variable warnings created by static assert?

Niklas Gürtler profclonk@gmail.com
Thu Jul 23 14:34:00 GMT 2015


Why don't you simply use a C++ static_assert?

static_assert(foo==bar, "foo should have been bar");


On 23.07.2015 14:50, Jeffrey Walton wrote:
> Our project has a static assert.  Its creating a noisy output at -Wall
> and above. We attempted to manage it with a GCC diagnostic block, but
> its not quite meeting expectations. The static assert creates a bunch
> of unused variable warnings.
>
> We have a UNUSED macro that casts to a void (its the only portable way
> I know to clear an unused warning), but we don't have a variable name
> to use with it.
>
> How can we clear a warning like `unused variable 'assert_26'
> [-Wunused-variable]|`?
>
> **********
> Here's the static assert:
>
> template <bool b>
> struct CompileAssert
> {
>      static char dummy[2*b-1];
> };
>
> #define COMPILE_ASSERT(assertion) COMPILE_ASSERT_INSTANCE(assertion, __LINE__)
> #define COMPILE_ASSERT_INSTANCE(assertion, instance) static
> CompileAssert<(assertion)> ASSERT_JOIN(assert_, instance)
> #define ASSERT_JOIN(X, Y) DO_ASSERT_JOIN(X, Y)
> #define DO_ASSERT_JOIN(X, Y) X##Y
>
> **********
> Here's how we try to manage it in one of out header files:
>
> #ifndef FOO_BAR
> #define FOO_BAR
>
> #define GCC_OPTIMIZE_AWARE ((__GNUC__ > 4 || (__GNUC__ == 4 &&
> __GNUC_MINOR__ >= 7)) || defined(__clang__))
>
> #if GCC_DIAGNOSTIC_AWARE
> # pragma GCC diagnostic push
> # pragma GCC diagnostic ignored "-Wunused-value"
> # pragma GCC diagnostic ignored "-Wunused-variable"
> #endif
>
> ....
> COMPILE_ASSERT(foo == bar);
>
> #if GCC_DIAGNOSTIC_AWARE
> # pragma GCC diagnostic pop
> #endif
>
> #endif // FOO_BAR
>
> **********
>
> Users include the header file, so we have no control over which
> options they place on the command line.
>
> Here's an example of the warning it generates with -Wall and GCC 4.8
> (even when guarded like above):
>
>      unused variable 'assert_26' [-Wunused-variable]|



More information about the Gcc-help mailing list