How to deal with unused variable with a BOOST_ASSERT check?
In serialization (boost/archive/iterators/mb_from_wchar.hpp) there is some code that generates a warning on release builds. We're always striving for warning-free code in all cases. Here's the snippet: std::codecvt_base::result r = m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend ); BOOST_ASSERT(std::codecvt_base::ok == r); Normally I'd say one should always check the result and throw an exception instead of relying on an assert. I'm sure consumers would like that more. That said, is there another macro that would allow me to change the code to do something like this? BOOST_ASSERT_ASSIGN_OR_IGNORE(std::codecvt_base::result r) m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend ); BOOST_ASSERT(std::codecvt_base::ok == r); where BOOST_ASSERT_ASSIGN_OR_IGNORE would expand to, assuming BOOST_ASSERT_ACTIVE is an indicator whether assert code is compiled in: #if BOOST_ASSERT_ACTIVE std::codecvt_base::result r = #else (void) #endif This would allow me to eliminate the warning without submitting a PR that changes the behavior of the code. If something like this does not exist, would it be useful for me to add it to boostorg/assert, or would folks recommend against encouraging this behavior and instead change it to use exceptions? Thanks, Jim
On 09/09/17 17:50, James E. King, III via Boost wrote:
That said, is there another macro that would allow me to change the code to do something like this?
BOOST_ASSERT_ASSIGN_OR_IGNORE(std::codecvt_base::result r) m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend ); BOOST_ASSERT(std::codecvt_base::ok == r);
where BOOST_ASSERT_ASSIGN_OR_IGNORE would expand to, assuming BOOST_ASSERT_ACTIVE is an indicator whether assert code is compiled in:
#if BOOST_ASSERT_ACTIVE std::codecvt_base::result r = #else (void) #endif
This would allow me to eliminate the warning without submitting a PR that changes the behavior of the code.
There is boost/core/ignore_unused.hpp that contains tools for suppressing warnings about unused variables or types. For example, the code above can be fixed by adding this line: boost::ignore_unused(r);
On Sat, Sep 9, 2017 at 7:50 AM, James E. King, III via Boost
BOOST_ASSERT_ASSIGN_OR_IGNORE(std::codecvt_base::result r) m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend ); BOOST_ASSERT(std::codecvt_base::ok == r);
Try: BOOST_VERIFY( m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend) == std::codecvt_base::ok); Reference: http://www.boost.org/doc/libs/1_64_0/libs/assert/assert.html
On Sat, Sep 9, 2017 at 7:50 AM, James E. King, III via Boost < boost@lists.boost.org> wrote:
In serialization (boost/archive/iterators/mb_from_wchar.hpp) there is some code that generates a warning on release builds. We're always striving for warning-free code in all cases.
Use -isystem instead of -I when including Boost or any other external headers. Emil
On 09/09/2017 16:50, James E. King, III via Boost wrote:
BOOST_ASSERT_ASSIGN_OR_IGNORE(std::codecvt_base::result r) m_codecvt_facet.out( m_mbs, & value, & value + 1, wend, m_buffer, m_buffer + sizeof(m_buffer), bend ); BOOST_ASSERT(std::codecvt_base::ok == r);
where BOOST_ASSERT_ASSIGN_OR_IGNORE would expand to, assuming BOOST_ASSERT_ACTIVE is an indicator whether assert code is compiled in:
#if BOOST_ASSERT_ACTIVE std::codecvt_base::result r = #else (void) #endif
This would allow me to eliminate the warning without submitting a PR that changes the behavior of the code. If something like this does not exist, would it be useful for me to add it to boostorg/assert, or would folks recommend against encouraging this behavior and instead change it to use exceptions?
I unconditionally write (void)r in my code after the assertion, and that shuts the compiler. If you need to do more complicated things when the assert is or not active, then use BOOST_ASSERT_IS_VOID: http://www.boost.org/doc/libs/1_65_1/libs/assert/doc/html/assert.html#boost_... Ion
participants (5)
-
Andrey Semashev
-
Emil Dotchevski
-
Ion GaztaƱaga
-
James E. King, III
-
Vinnie Falco