diff --git a/doc/core.qbk b/doc/core.qbk index 867055f..3926983 100644 --- a/doc/core.qbk +++ b/doc/core.qbk @@ -37,6 +37,10 @@ Currently, the Core library contains: [[@../../enable_if.html enable_if]] [`boost::enable_if`] ] + [ + [[link core.ignore_unused ignore_unused]] + [`boost::ignore_unused`] + ] [ [[link core.lightweight_test lightweight_test]] [`BOOST_TEST, BOOST_ERROR, BOOST_TEST_EQ, BOOST_TEST_NE, @@ -75,6 +79,7 @@ Currently, the Core library contains: [include:core addressof.qbk] [include:core checked_delete.qbk] [include:core explicit_operator_bool.qbk] +[include:core ignore_unused.qbk] [include:core lightweight_test.qbk] [include:core no_exceptions_support.qbk] [include:core noncopyable.qbk] diff --git a/doc/ignore_unused.qbk b/doc/ignore_unused.qbk new file mode 100644 index 0000000..b083c77 --- /dev/null +++ b/doc/ignore_unused.qbk @@ -0,0 +1,29 @@ +[section:ignore_unused Header ] + +The header `` defines the function template +`boost::ignore_unused()`. It may be used to suppress the "unused variable" or +"unused local typedefs" compiler warnings when the variable or typedef +can't be removed or commented out, e.g. when some blocks of the code are +conditionally activated. C++11 variadic templates are used if they're supported, +otherwise they're emulated with overloads. + +`boost::ignore_unused()` was contributed by Adam Wulkiewicz. + +Usage + + boost::ignore_unused(v1, v2, v3); + boost::ignore_unused(); + +Example + + int fun( int foo, int bar ) + { + boost::ignore_unused(bar); + #ifdef ENABLE_DEBUG_OUTPUT + if ( foo < bar ) + std::cerr << "warning! foo < bar"; + #endif + return foo + 2; + } + +[endsect] diff --git a/include/boost/core/ignore_unused.hpp b/include/boost/core/ignore_unused.hpp new file mode 100644 index 0000000..22047c2 --- /dev/null +++ b/include/boost/core/ignore_unused.hpp @@ -0,0 +1,70 @@ +// Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland. +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_CORE_IGNORE_UNUSED_HPP +#define BOOST_CORE_IGNORE_UNUSED_HPP + +#include + +namespace boost { + +#ifndef BOOST_NO_CXX11_VARIADIC_TEMPLATES + +template +inline void ignore_unused(Ts const& ...) +{} + +template +inline void ignore_unused() +{} + +#else + +template +inline void ignore_unused(T1 const&) +{} + +template +inline void ignore_unused(T1 const&, T2 const&) +{} + +template +inline void ignore_unused(T1 const&, T2 const&, T3 const&) +{} + +template +inline void ignore_unused(T1 const&, T2 const&, T3 const&, T4 const&) +{} + +template +inline void ignore_unused(T1 const&, T2 const&, T3 const&, T4 const&, T5 const&) +{} + +template +inline void ignore_unused() +{} + +template +inline void ignore_unused() +{} + +template +inline void ignore_unused() +{} + +template +inline void ignore_unused() +{} + +template +inline void ignore_unused() +{} + +#endif + +} // namespace boost + +#endif // BOOST_CORE_IGNORE_UNUSED_HPP diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a07562d..88947a6 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -36,4 +36,8 @@ test-suite "core" [ compile-fail explicit_operator_bool_compile_fail_conv_pvoid.cpp ] [ compile-fail explicit_operator_bool_compile_fail_delete.cpp ] [ compile-fail explicit_operator_bool_compile_fail_shift.cpp ] + + [ compile ignore_unused_test.cpp : gcc:"-Wunused-variable -Wunused-local-typedefs -Werror" + clang:"-Wunused-variable -Werror" + msvc:"/we4100 /we4101" ] ; diff --git a/test/ignore_unused_test.cpp b/test/ignore_unused_test.cpp new file mode 100644 index 0000000..4c81aaf --- /dev/null +++ b/test/ignore_unused_test.cpp @@ -0,0 +1,64 @@ +// Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland. +// +// Use, modification and distribution is subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include + +int main() +{ + { + int a; + boost::ignore_unused(a); + } + { + int a, b; + boost::ignore_unused(a, b); + } + { + int a, b, c; + boost::ignore_unused(a, b, c); + } + { + int a, b, c, d; + boost::ignore_unused(a, b, c, d); + } + { + int a, b, c, d, e; + boost::ignore_unused(a, b, c, d, e); + } + + { + typedef int a; + boost::ignore_unused(); + } + { + typedef int a; + typedef int b; + boost::ignore_unused(); + } + { + typedef int a; + typedef int b; + typedef int c; + boost::ignore_unused(); + } + { + typedef int a; + typedef int b; + typedef int c; + typedef int d; + boost::ignore_unused(); + } + { + typedef int a; + typedef int b; + typedef int c; + typedef int d; + typedef int e; + boost::ignore_unused(); + } + + return 0; +}