Boost.Test检查指针是否为空

Bar*_*rth 7 c++ boost c++11 boost.test

我有以下测试:

BOOST_CHECK_NE(pointer, nullptr);
Run Code Online (Sandbox Code Playgroud)

由于编译失败

/xxx/include/boost/test/tools/detail/print_helper.hpp:50:14:错误:'operator <<'的模糊重载(操作数类型是'std :: ostream {aka std :: basic_ostream}'和'的std :: nullptr_t")

有什么问题,我应该如何测试空指针?

Rei*_*ica 8

指针非空的最简单检查是:

BOOST_CHECK(pointer);
Run Code Online (Sandbox Code Playgroud)

空指针隐式转换false为非空指针隐式转换为true.

至于你的用例的问题是:nullptr不是指针类型,它是类型std::nullptr_t.它可以转换为任何指针类型(或指向成员类型的指针).但是,<<插入std::nullptr_t流中没有过载.您必须转换nullptr为适当的指针类型才能使其工作.