有没有人看过Andrei Alexandrescu在GoingNative2013中关于爆炸元组的讨论呢?
这是我不太关注的代码片段:
template <class F, class... Ts>
auto explode(F&& f, const tuple<Ts...>& t)
-> typename result_of<F(Ts...)>::type
{
return Expander<sizeof...(Ts),
typename result_of<F(Ts...)>::type,
F,
const tuple<Ts...>&>::expand(f, t);
}
Run Code Online (Sandbox Code Playgroud)
结果中的F(Ts ...)让我很烦恼.我的意思是:F不代表功能类型吗?我知道R(Ts ...)很好,但这里的R是一个返回类型,但是在R应该使用F的位置,那就是让我发疯的事情......
任何人都可以帮我理解奇怪的F(Ts ...)吗?
以下是Andrei Alexandrescu的演讲链接:http: //channel9.msdn.com/Events/GoingNative/2013/The-Way-of-the-Exploding-Tuple
您想问的问题可能与此重复:为什么 std::result_of 将(不相关的)函数类型作为类型参数?
我们来剖析一下:
std::result_of<F(Ts...)>::type
Run Code Online (Sandbox Code Playgroud)
所以,在 的某个地方namespace std,我们有一个类模板result_of<>。它需要一个模板类型参数;即,它看起来基本上是这样的:
template<typename Foo>
struct result_of
{
typedef FOOBARBAZ type;
};
Run Code Online (Sandbox Code Playgroud)
好的,我们用参数实例化这个模板F(Ts...)。这是不寻常的语法!您可能知道这Ts是一个参数包,因此Ts...括号内的内容将在编译时扩展为逗号分隔的类型列表,例如int, double, bool。所以我们有F(int, double, bool). 好的,这是一个函数类型。
正如int(char)“函数获取char和返回”的意思一样, “函数获取和返回”的意思int也是如此。F(int, double, bool)int, double, boolF
“但是等等,”你说。“我以为F已经是我的功能类型了!”
是的。F是你的函数类型。但实际上,std::result_of所期望的类型是该函数类型包装在另一个函数类型中。详细说明:
typedef int (*F)(char);
typedef F G(char);
static_assert(std::is_same< std::result_of<G>::type, int >::value);
static_assert(std::is_same< std::result_of<F(char)>::type, int >::value);
static_assert(std::is_same< std::result_of<int (*(char))(char)>::type, int >::value);
Run Code Online (Sandbox Code Playgroud)
上面的每一行都是完全相同的:F(char)只是一种更美观的书写方式int (*(char))(char)。当然,你不能总是逃脱它,因为有时F是无法从函数返回的函数类型:
typedef int F(char);
std::result_of<F(char)>; // fails to compile
Run Code Online (Sandbox Code Playgroud)
正如 @Simple 在评论中所写,std::result_of<F(Ts...)>::type总是可以用不太聪明但也不那么令人困惑的表达来替换
decltype( std::declval<F>() ( std::declval<Ts>()... ) )
Run Code Online (Sandbox Code Playgroud)
即,“使用类型 的decltype参数调用类型的值的结果。这里,没有古怪的高级函数类型;一切都按照您自然期望的方式工作。就我个人而言,我可能会使用在我自己的代码中使用这种方法,只是因为它更容易理解;但我想有些人会更喜欢这种方法,因为它表面上看起来更简单,并且受到标准的祝福。对每个人来说都是如此。:)FTs...decltypestd::result_of
| 归档时间: |
|
| 查看次数: |
522 次 |
| 最近记录: |