man*_*anu 15 c++ templates types 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
呢?
这取决于,因为在某些情况下会导致F
和之间产生不同的效果decltype(f)
。
例如,F
可以显式指定为数组或函数类型,而函数参数的类型将调整为指针。然后F
给出decltype(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
...
}
Run Code Online (Sandbox Code Playgroud)