Boost.Test 检查指针是否为空

Boost.Test check whether a pointer is null

我有以下测试:

BOOST_CHECK_NE(pointer, nullptr);

由于

编译失败

/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’)

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

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

BOOST_CHECK(pointer);

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

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

如错误消息中所述,nullptr 有不明确的重载。

BOOST_CHECK(pointer);

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

应该完成这项工作。