小编man*_*anu的帖子

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

https://github.com/stlab/libraries/blob/main/stlab/concurrency/main_executor.hpp中,我读到

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;
        });
    }
};
Run Code Online (Sandbox Code Playgroud)

有什么意义decltype(f),为什么不简单地使用F呢?

c++ templates types decltype

15
推荐指数
1
解决办法
661
查看次数

带有用户类型的C++ 20模板<auto>导致GCC 9中的T/const T类型不匹配

我正在尝试使用自定义类型的非类型模板.

struct T {};

template <auto value> struct U {};

template <auto value> 
void f (U <value>) {}

int main()
{
    constexpr T t;
    f    (U<1>{});  // OK
    f<t> (U<t>{});  // OK
    f    (U<t>{});  // Error
}
Run Code Online (Sandbox Code Playgroud)

模板参数推断失败,gcc trunk使用-std = c ++ 2a获取

yop.cpp:10:5: note:   template argument deduction/substitution failed:
yop.cpp:19:21: note:   mismatched types ‘T’ and ‘const T’
   19 |         f    (U<t>{});  // Error
      |                     ^
Run Code Online (Sandbox Code Playgroud)

我错过了什么或这是一个错误吗?

c++ gcc templates c++20

8
推荐指数
1
解决办法
214
查看次数

C++ 为什么约束算法(例如 std::ranges::merge)也返回输入范围的结尾?

std::ranges::merge(例如)返回包含合并范围结束的一组迭代器,显然,但也包含两个输入范围的结束。Cppreference 说(https://en.cppreference.com/w/cpp/algorithm/ranges

此外,大多数算法的返回类型已更改为返回在算法执行期间计算的所有潜在有用信息。

返回输入范围的末尾有什么意义?

c++ c++20 std-ranges

5
推荐指数
1
解决办法
96
查看次数

标签 统计

c++ ×3

c++20 ×2

templates ×2

decltype ×1

gcc ×1

std-ranges ×1

types ×1