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:错误:'operator<<'的模棱两可的重载(操作数类型为'std :: ostream {aka std :: basic_ostream }'和'std :: nullptr_t')
/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 ?
推荐答案
最简单的检查指针是否为非空的方法是:
The easiest check for a pointer being non-null is this:
BOOST_CHECK(pointer);
空指针隐式转换为false
,非空指针隐式转换为true
.
A null pointer implicitly converts to false
, a non-null pointer implicitly converts to true
.
关于用例的问题是:nullptr
不是指针类型,而是std::nullptr_t
类型.可以将其转换为任何指针类型(或指向成员类型的指针).但是,将std::nullptr_t
插入流中没有<<
的重载.您必须将nullptr
强制转换为适当的指针类型,以使其起作用.
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.
这篇关于Boost.Test检查指针是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!