Boost.Test检查指针是否为空 [英] Boost.Test check whether a pointer is null

查看:128
本文介绍了Boost.Test检查指针是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了以下测试:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆