Boost.Test check whether a pointer is null

后端 未结 2 1533
情话喂你
情话喂你 2021-01-12 05:56

I have the following test :

BOOST_CHECK_NE(pointer, nullptr);

The compilation fails due to

/xxx/include/boost/test

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-12 06:31

    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.

提交回复
热议问题