简体   繁体   English

理解这种`const&`专业化的必要性

[英]Understanding the need for this `const&` specialization

In the Guidelines Support Library there is a class called final_action (essentially the well known ScopeGuard). 在指南支持库中有一个名为final_action的类(本质上是众所周知的ScopeGuard)。 There are 2 free-standing convenience functions to generate this templated class: 有2个独立的便利函数来生成这个模板化的类:

// finally() - convenience function to generate a final_action
template <class F>
inline final_action<F> finally(const F& f) noexcept
{
    return final_action<F>(f);
}

template <class F>
inline final_action<F> finally(F&& f) noexcept
{
    return final_action<F>(std::forward<F>(f));
}

(source: https://github.com/Microsoft/GSL/blob/64a7dae4c6fb218a23b3d48db0eec56a3c4d5234/include/gsl/gsl_util#L71-L82 ) (来源: https//github.com/Microsoft/GSL/blob/64a7dae4c6fb218a23b3d48db0eec56a3c4d5234/include/gsl/gsl_util#L71-L82

What is the need for the first one? 第一个需要什么? If we only had the second one (using the forwarding , aka universal, references) wouldn't it do the same thing? 如果我们只有第二个(使用转发,也就是通用,引用)它不会做同样的事情吗?

Let's consider the perfectly-forwarding version: 让我们考虑完美转发版本:

  • When called with an rvalue, it will return final_action<F>(static_cast<F&&>(f)) . 当使用rvalue调用时,它将返回final_action<F>(static_cast<F&&>(f))

  • When called with an lvalue, it will return final_action<F&>(f) . 当使用左值调用时,它将返回final_action<F&>(f)

Let's now consider the const F& overload: 我们现在考虑const F& overload:

  • When called both an lvalue or rvalue, it will return final_action<F>(f) . 当同时调用左值或右值时,它将返回final_action<F>(f)

As you can see, there is an important difference: 如您所见,有一个重要的区别:

  • Passing a non- const lvalue reference to finally will produce a wrapper that stores a F& 传递一个非const左值参照finally将产生存储了一个包装F&

  • Passing a const lvalue reference to finally will produce a wrapper that stores a F const lvalue引用传递给finally将生成一个存储F的包装器

live example on wandbox wandbox上的实例


I am not sure why it was deemed necessary to have the const F& overload. 我不确定为什么认为有必要让const F& over。

This is the implementation of final_action : 这是final_action的实现:

template <class F>
class final_action
{
public:
    explicit final_action(F f) noexcept : f_(std::move(f)), invoke_(true) {}

    final_action(final_action&& other) noexcept 
        : f_(std::move(other.f_)), invoke_(other.invoke_)
    {
        other.invoke_ = false;
    }

    final_action(const final_action&) = delete;
    final_action& operator=(const final_action&) = delete;

    ~final_action() noexcept
    {
        if (invoke_) f_();
    }

private:
    F f_;
    bool invoke_;
};

Unless I am missing something, instanting final_action<F&> doesn't really make sense, as f_(std::move(f)) will not compile. 除非我遗漏了某些内容,否则final_action<F&>并没有多大意义,因为f_(std::move(f))将无法编译。

live example on wandbox wandbox上的实例

So I think this should have just been: 所以我认为这应该是:

template <class F>
inline final_action<F> finally(F&& f) noexcept
{
    return final_action<std::decay_t<F>>(std::forward<F>(f));
}

Ultimately, I think that the implementation of finally in GSL incorrect/unoptimal (ie redundant, has code repetition). 最终,我认为finally在GSL中的实现不正确/不理想(即冗余,有代码重复)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM