【发布时间】:2016-06-15 11:46:32
【问题描述】:
如本页http://en.cppreference.com/w/cpp/thread/async所示,C++14中std::async的签名之一已从C++11版本更改
template< class Function, class... Args>
std::future<typename std::result_of<Function(Args...)>::type>
async( Function&& f, Args&&... args );
到
template< class Function, class... Args>
std::future<std::result_of_t<std::decay_t<Function>(std::decay_t<Args>...)>>
async( Function&& f, Args&&... args );
更改是在传递给std::result_of 之前应用于函数和参数类型的std::decay_ts(删除引用和cv 限定符并将数组/函数衰减为指针)。我不太明白为什么衰变是有用的。例如,对于函数类型Fn(可能是闭包类的类型别名),传递Fn、Fn&&、const Fn& 等似乎都会产生相同的结果。
谁能给我一个衰变有用的具体例子吗?
更新:例如,这段代码:
#include <iostream>
#include <type_traits>
int main()
{
auto fn = [](auto x) -> int { return x + 1; };
using Fn = decltype(fn);
using FnRef = Fn&;
using FnCRef = const Fn&;
using FnRRef = Fn&&;
std::cout << std::boolalpha
<< std::is_same<int, std::result_of_t<Fn(int)>>::value << '\n'
<< std::is_same<int, std::result_of_t<FnRef(int)>>::value << '\n'
<< std::is_same<int, std::result_of_t<FnCRef(int)>>::value << '\n'
<< std::is_same<int, std::result_of_t<FnRRef(int)>>::value << '\n';
return 0;
}
将打印出四个trues。
【问题讨论】:
-
我认为它有一个引用函数类型的意义。
-
@101010
decay不去掉引用限定吗? -
@101010 请看我的更新。