gpt4 book ai didi

c++ - Boost.Test 检查指针是否为空

转载 作者:可可西里 更新时间:2023-11-01 18:26:14 24 4
gpt4 key购买 nike

我有以下测试:

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到适当的指针类型以使其工作。

关于c++ - Boost.Test 检查指针是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37673724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com