Boost logo

Boost :

Subject: [boost] How to deal with unused variable with a BOOST_ASSERT check?
From: James E. King, III (jking_at_[hidden])
Date: 2017-09-09 14:50:08


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk