为 nullptr 禁用 BOOST_CHECK

Disable BOOST_CHECK for nullptr

使用 Boost Test 1.64 之前的版本,您不能这样做:

SomeType* a_pointer = getPointer();
BOOST_CHECK_EQUAL(a_pointer, nullptr);

这是因为 nullptr_t 有不明确的重载:Boost issue #12778 and 。正如问题的答案一样,这很容易解决:

BOOST_CHECK(!a_pointer); // rely on boolean casting, or...
// cast nullptr away from nullptr_t
BOOST_CHECK_EQUAL(a_pointer, static_cast<SomeType*>(nullptr));

但是,如果您支持多个提升版本,BOOST_CHECK_EQUAL(a_pointer, nullptr) 很容易在较新的平台上溜走并破坏旧平台。

这里的一个解决方案是强制使用具有旧 Boost 版本的 CI 平台(这对于其他原因也很有用,特别是当支持的 Boost 版本扩展到 1.59 之前时,Boost Test 3 发生了很大变化!) .

但是,仅依靠 CI 来捕获这是 OODA 循环中的一个很大的延迟(与本地编译器故障相比)并且需要网络访问和简单但烦人的 VCS 舞蹈来修补细小的更改并重新提交作业。

有没有办法让它编译失败,即使 Boost 版本支持它?

在 Boost 测试中,这是在 commit 229e71199 for v1.64, using the print_log_value customization point:

中实现的
template<>
struct BOOST_TEST_DECL print_log_value<std::nullptr_t> {
    void operator()( std::ostream& ostr, std::nullptr_t t ) {
        ostr << "nullptr";
    }
};

不可能 "undef" 在不同的翻译单元中定义的函数(除非有一些非常讨厌的预处理器黑客攻击)。因此,如果您尝试使用该函数,"damage" 函数并导致编译失败是不可能的。

但是我们有两个更好的选择:使用 Boost 测试方法来避免打印这种类型,或者自己做。


避免打印 nullptr_t

您使用现有的 Boost 自定义点来防止记录类型:BOOST_TEST_DONT_PRINT_LOG_VALUE:

// in your common test header
BOOST_TEST_DONT_PRINT_LOG_VALUE( std::nullptr_t )

这是做什么的,因为在 Boost 1.59 中定义了一个 print_log_value 函数 什么都不做:

#define BOOST_TEST_DONT_PRINT_LOG_VALUE( the_type )         \
namespace boost{ namespace test_tools{ namespace tt_detail{ \
template<>                                                  \
struct print_log_value<the_type > {                         \
    void    operator()( std::ostream&, the_type const& ) {} \
};                                                          \
}}}                                                         \                                                     

在 1.59 (commit bae8de14b) 之前,它的定义有所不同(一开始在 tt_detail 中没有),但思路是一样的。这意味着使用此宏它将至少恢复到 1.58 和更早版本。

但是,因为在1.64中定义了print_log_value函数,所以如果只添加上面的宏,从1.64开始就会出现重定义错误:从DONT_PRINT 宏,以及打印 "nullptr" 的宏。所以你可以用相关的 Boost 版本来保护它:

#if BOOST_VERSION < 106400
    BOOST_TEST_DONT_PRINT_LOG_VALUE( std::nullptr_t )
#endif

现在它将避免在 Boost < 1.64 上打印 nullptr,它将在 1.64+ 上打印:

[ != 0xdeadbeef]        // < 1.64, using BOOST_TEST_DONT_PRINT_LOG_VALUE
[nullptr != 0xdeadbeef] // 1.64 +, using built-in implementation

如果您真的不关心旧 Boost 版本的日志记录的美感,这可能就足够了。


自己动手

您还可以实施您自己的print_log_value自定义点。但是,请注意 1.59 之前的命名空间不同,我们应该只为 <1.64 这样做,因为我们将重新定义函数:

// You don't need this bit if you don't support Boost Test <1.59.
#if BOOST_VERSION >= 105900
#    define BOOST_TEST_PRINT_NAMESPACE_OPEN namespace boost { namespace test_tools { namespace tt_details {
#    define BOOST_TEST_PRINT_NAMESPACE_CLOSE }}}
#else
#    define BOOST_TEST_PRINT_NAMESPACE_OPEN namespace boost { namespace test_tools {
#    define BOOST_TEST_PRINT_NAMESPACE_CLOSE }}
#endif

#if BOOST_VERSION < 106400

BOOST_TEST_PRINT_NAMESPACE_OPEN

template<>
struct print_log_value<nullptr_t> {
    inline void operator()(std::ostream& os, nullptr_t const& p) {
        os << "nullptr";
    }
};

BOOST_TEST_PRINT_NAMESPACE_CLOSE

#endif // End <1.64 condition

现在它会打印同样的东西:

[nullptr != 0xdeadbeef] // < 1.64, using DIY implementation
[nullptr != 0xdeadbeef] // 1.64 +, using built-in implementation