Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.Test check whether a pointer is null

I have the following test :

BOOST_CHECK_NE(pointer, nullptr);

The compilation fails due to

/xxx/include/boost/test/tools/detail/print_helper.hpp:50:14: error: ambiguous overload for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream}’ and ‘std::nullptr_t’)

What is wrong and how should I test for null pointers ?

like image 626
Barth Avatar asked Jun 07 '16 08:06

Barth


Video Answer


2 Answers

The easiest check for a pointer being non-null is this:

BOOST_CHECK(pointer);

A null pointer implicitly converts to false, a non-null pointer implicitly converts to true.

As to what the problem with your use case is: nullptr is not a pointer type, it's of type std::nullptr_t. It can be converted to any pointer type (or pointer to member type). However, there is no overload of << for inserting std::nullptr_t into a stream. You would have to cast nullptr to the appropriate pointer type to make it work.

like image 75
Angew is no longer proud of SO Avatar answered Sep 21 '22 19:09

Angew is no longer proud of SO


As mentioned in error message, nullptr has ambiguous overloads.

BOOST_CHECK(pointer);

or

BOOST_CHECK_NE(pointer, static_cast<decltype(pointer)>(nullptr));

should do the job.

like image 20
Jarod42 Avatar answered Sep 22 '22 19:09

Jarod42