c++boost.gif (8819 bytes)Header boost/disable_warning.hpp

Contents

Macro BOOST_NO_UNUSED_VARIABLE_WARNING

Macro BOOST_NO_UNUSED_VARIABLE_WARNING prevents compiler warnings about unused variables for the given variable binding, either a function argument or local variable.

#define BOOST_NO_UNUSED_VARIABLE_WARNING(v)

A common problem C++ programmers run into while writing portable code are "unused variable warnings" from helpful compilers. Although this is a good warning to be aware of, there are some circumstances when it just gets in the way. For example when doing debugging code:

template <class C, class T>
void f(C & c, const T & v)
{
  size_t s0 = v.size(); // warns in NDEBUG mode
  c.push_back(v);
  size_t s1 = v.size(); // warns in NDEBUG mode
  assert(s0 < s1);
}

Using this macro has two important benefits: it prevents the warning from occurring, and it documents the fact that the variables are intentionally not used. For example:

template <class C, class T>
void f(C & c, const T & v)
{
  size_t s0 = v.size();
  c.push_back(v);
  size_t s1 = v.size();
  BOOST_NO_UNUSED_VARIABLE_WARNING(s0);
  BOOST_NO_UNUSED_VARIABLE_WARNING(s1);
  assert(s0 < s1);
}

Contributed by Rene Rivera.


Revised  30 October, 2003

© Copyright Rene Rivera 2003.

Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at www.boost.org/LICENSE_1_0.txt)