为什么在模板参数上使用velltype?

发布于 2025-02-09 20:11:42 字数 623 浏览 1 评论 0 原文

,我阅读了

struct main_executor_type {
    using result_type = void;

    template <typename F>
    void operator()(F f) const {
        using f_t = decltype(f);

        dispatch_async_f(dispatch_get_main_queue(), new f_t(std::move(f)), [](void* f_) {
            auto f = static_cast<f_t*>(f_);
            (*f)();
            delete f;
        });
    }
};

exltype(f)的重点,为什么不简单地使用 f

In https://github.com/stlab/libraries/blob/main/stlab/concurrency/main_executor.hpp, I read

struct main_executor_type {
    using result_type = void;

    template <typename F>
    void operator()(F f) const {
        using f_t = decltype(f);

        dispatch_async_f(dispatch_get_main_queue(), new f_t(std::move(f)), [](void* f_) {
            auto f = static_cast<f_t*>(f_);
            (*f)();
            delete f;
        });
    }
};

What is the point of the decltype(f), why no simply use F?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故笙诉离歌 2025-02-16 20:11:43

这取决于这是由于某些情况会导致 f decltype(f)之间的效果不同。

例如,可以将 f 明确指定为数组或功能类型,并且功能参数的类型将调整为指针。然后 f exttype(f)给出不同的结果。

template <typename F> // suppose F is specified as array of T or function type F
void operator()(F f) const {
    using f_t = decltype(f); // f_t will be pointer to T or pointer to F
    ...
}

It depends, since there're some cases leading to different effect between F and decltype(f).

For example, F could be specified explicitly as array or function type, and the type of function parameter will be adjusted to pointer. Then F and decltype(f) give different result.

template <typename F> // suppose F is specified as array of T or function type F
void operator()(F f) const {
    using f_t = decltype(f); // f_t will be pointer to T or pointer to F
    ...
}
桃气十足 2025-02-16 20:11:43

我看不到此特定代码示例的任何有用的方案(如果我错了,请纠正我),但是如果将参数声明为CV qualified,例如。然后,使用f_t = exttype(f); 将评估 const f ,而使用f_t = f; 将删除简历预选赛并评估至 f

考虑以下简约示例以更好地理解:

#include <type_traits>

template <typename T>
void foo(const T t) {
    using X = decltype(t);
    static_assert(std::is_const_v<X>); // will fail if using X = T;
}

int main() {
    const int a = 123;
    foo(a); // T will be deduced to int, i.e. cv qualifiers are removed
}

I cannot see any useful scenario for this particular code example (please correct me if I'm wrong), but it would look differently if the parameter would have been declared as cv-qualified, e.g. const F f. Then, using f_t = decltype(f); would evaluate to const F, while using f_t = F; would remove cv qualifiers and evaluate to F.

Consider the following minimalistic example for better understanding:

#include <type_traits>

template <typename T>
void foo(const T t) {
    using X = decltype(t);
    static_assert(std::is_const_v<X>); // will fail if using X = T;
}

int main() {
    const int a = 123;
    foo(a); // T will be deduced to int, i.e. cv qualifiers are removed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文