3

在 Microsoft实施指南支持库中,我看到以下代码:

template<class T>
class not_null {
    ...
    template <typename U, typename = std::enable_if_t<std::is_convertible<U, T>::value>>
    constexpr explicit not_null(U&& u) : ptr_(std::forward<U>(u)) {
        Expects(ptr_ != nullptr);
    }
    ...
    constexpr T get() const {
        Ensures(ptr_);
        return ptr_;
    }
    ...
    T ptr_;
}

所有gsl::not_null可能使用指针的构造函数都会检查这些指针是否为空,但我们仍会在每次ptr_取消引用时检查指针 ( )的存储值是否为空。鉴于在 C++ 中我们通常不会为不需要的东西付费,为什么我们要进行此检查?

UP:确保按如下方式实现(使用默认标志):

#define GSL_LIKELY(x) (!!(x))
...
#define GSL_CONTRACT_CHECK(type, cond)                         \
(GSL_LIKELY(cond) ? static_cast<void>(0) : gsl::details::terminate())
...
#define Ensures(cond) GSL_CONTRACT_CHECK("Postcondition", cond)
4

1 回答 1

2

评论已经给出了为什么not_null::get()不希望删除空检查的想法。主要问题是更改允许在移动后取消引用智能指针。

例如,请参阅以下关于启用使用的 PR 的讨论not_null<unique_ptr>以及更改如何与从中删除 null 检查不兼容not_null::get()

https://github.com/Microsoft/GSL/pull/675

至于性能问题,编译器优化器应该能够删除许多空检查,但当然不是全部。如果某些检查未删除但似乎可以删除,我们应该修复编译器优化。

于 2018-07-03T21:37:25.350 回答