Boost.Test check if pointer is null

I have the following test:

BOOST_CHECK_NE(pointer, nullptr); 

Compilation failed due to

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

What is wrong and how should I test null pointers?

+6
source share
2 answers

The simplest check for a pointer that is not null is this:

 BOOST_CHECK(pointer); 

A null pointer is implicitly converted to false , a non-bubble pointer is implicitly converted to true .

As for the problem with your use case: nullptr not a pointer type, then the type is std::nullptr_t . It can be converted to any type of pointer (or a pointer to a member type). However, there is no << overload to insert std::nullptr_t into the stream. You will need to attach nullptr to the appropriate pointer type for it to work.

+5
source

As indicated in the error message, nullptr has ambiguous overloads.

 BOOST_CHECK(pointer); 

or

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

must do the job.

+3
source

All Articles