Boost logo

Boost Users :

Subject: [Boost-users] [test] BOOST_CHECK_EQUAL comparisons with NULL don't compile
From: James Mathiesen (james-mathiesen_at_[hidden])
Date: 2012-09-07 12:32:06


This use case seems really common and I was surprised it gave me problems.

I assume that nullptr_t in C++11 fixes this but then I was wondering
if the cases were basic and common enough it would make sense to have
a BOOST_CHECK_NULL / BOOST_CHECK_NOT_NULL and the assorted variants.

james

---
#define  BOOST_TEST_DYN_LINK
#define  BOOST_TEST_MODULE      Hello
#include <boost/test/unit_test.hpp>
// main() etc... is defined by the headers
BOOST_AUTO_TEST_CASE( null_compare )
{
    const char *ptr = NULL;
    //
    // Equality comparisons with NULL Fail to compile using boost 1.46
    // and g++-4.6
    //
    // /usr/include/boost/test/test_tools.hpp:536:20:
    //     error: ISO C++ forbids comparison between pointer and
    //     integer [-fpermissive]
    //
    // Which seems to be flagging this
    //
    //  template <class Left, class Right>
    //  predicate_result equal_impl( Left const& left, Right const& right )
    //  {
    //      return left == right;
    //  }
    //
    // Where Left is of type "const char *" and Right is of type "long int".
    //
    BOOST_CHECK_EQUAL(ptr, NULL);
    //
    // these tests compile and pass
    //
    BOOST_CHECK(ptr == NULL);
    BOOST_CHECK_EQUAL(ptr, ptr);
    BOOST_CHECK_EQUAL(ptr, (void *)ptr);
    BOOST_CHECK_EQUAL(ptr, (void *)NULL);
}
...
example.cc:33:1: error: passing NULL to non-pointer argument 9 of
‘bool boost::test_tools::tt_detail::check_frwd(Pred, const
boost::unit_test::lazy_ostream&, boost::test_tools::const_string,
std::size_t, boost::test_tools::tt_detail::tool_level,
boost::test_tools::tt_detail::check_type, const Arg0&, const char*,
const Arg1&, const char*) [with Pred =
boost::test_tools::tt_detail::equal_impl_frwd, Arg0 = const char*,
Arg1 = long int, boost::test_tools::const_string =
boost::unit_test::basic_cstring<const char>, std::size_t = long
unsigned int]’ [-Werror=conversion-null]
example.cc:33:1: error: passing NULL to non-pointer argument 9 of
‘bool boost::test_tools::tt_detail::check_frwd(Pred, const
boost::unit_test::lazy_ostream&, boost::test_tools::const_string,
std::size_t, boost::test_tools::tt_detail::tool_level,
boost::test_tools::tt_detail::check_type, const Arg0&, const char*,
const Arg1&, const char*) [with Pred =
boost::test_tools::tt_detail::equal_impl_frwd, Arg0 = const char*,
Arg1 = long int, boost::test_tools::const_string =
boost::unit_test::basic_cstring<const char>, std::size_t = long
unsigned int]’ [-Werror=conversion-null]
In file included from /usr/include/boost/test/unit_test.hpp:19:0,
                 from example.cc:7:
/usr/include/boost/test/test_tools.hpp: In function
‘boost::test_tools::predicate_result
boost::test_tools::tt_detail::equal_impl(const Left&, const Right&)
[with Left = const char*, Right = long int]’:
/usr/include/boost/test/test_tools.hpp:560:40:   instantiated from
‘boost::test_tools::predicate_result
boost::test_tools::tt_detail::equal_impl_frwd::call_impl(const Left&,
const Right&, mpl_::false_) const [with Left = const char*, Right =
long int, mpl_::false_ = mpl_::bool_<false>]’
/usr/include/boost/test/test_tools.hpp:575:56:   instantiated from
‘boost::test_tools::predicate_result
boost::test_tools::tt_detail::equal_impl_frwd::operator()(const Left&,
const Right&) const [with Left = const char*, Right = long int]’
/usr/include/boost/test/test_tools.hpp:523:1:   instantiated from
‘bool boost::test_tools::tt_detail::check_frwd(Pred, const
boost::unit_test::lazy_ostream&, boost::test_tools::const_string,
std::size_t, boost::test_tools::tt_detail::tool_level,
boost::test_tools::tt_detail::check_type, const Arg0&, const char*,
const Arg1&, const char*) [with Pred =
boost::test_tools::tt_detail::equal_impl_frwd, Arg0 = const char*,
Arg1 = long int, boost::test_tools::const_string =
boost::unit_test::basic_cstring<const char>, std::size_t = long
unsigned int]’
example.cc:33:1:   instantiated from here
/usr/include/boost/test/test_tools.hpp:536:20: error: ISO C++ forbids
comparison between pointer and integer [-fpermissive]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net