为什么不是std :: result_of< int(int)> :: type valid? [英] Why isn't std::result_of<int(int)>::type valid?

查看:154
本文介绍了为什么不是std :: result_of< int(int)> :: type valid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读以下相关问题:

I have read the following related questions:


  1. std :: result_of simple function

  2. decltype,result_of或typeof?

  1. std::result_of simple function
  2. decltype, result_of, or typeof?

cppreference.com上的 std :: result_of 页面。

所有这些都似乎表明我应该能够使用:

All of them seem to indicate that I should be able to use:

 std::result_of<int(int)>::type v1 = 10;
However, when I tried building the following program using g++ 4.9.2

我收到LINE A和LINE B的错误消息。错误消息是:

#include <type_traits> int foo() { return 0; } int main() { std::result_of<int(int)>::type v1 = 10; // LINE A std::result_of<decltype(foo)>::type v2 = 20; // LINE B return 0; }

I get error messages for "LINE A" and "LINE B". The error messages are:

我用于编译的命令:

The command I used to compile:

FWIW,使用

FWIW, using

没有影响。

似乎我不明白应该如何使用 result_of

didn't make a difference.

你能解释为什么我


推荐答案

正如你已经发布的链接中所述, result_of 必须是可调用类型或对函数的引用。

As stated in the link you have already posted, the first part of the argument to result_of must be a callable type or a reference to a function.

假设您有

struct Callable
{
    int operator()(double);
    void operator()(int);
};

然后 result_of 可帮助您确定返回类型,如果你知道参数的类型。对于上述示例:

then result_of helps you determining the return type, if you know the type of the arguments. For the example above:

result_of<Callable(int)>::type == void    ... per definition
result_of<Callable(double)>::type == int  ... per definition
result_of<Callable(char)>::type == void   ... the int-overload matches better
result_of<Callable(float)>::type == int   ... the double-overload matches better


$ b b

为了找到函数 foo 的返回类型,你必须通过函数引用:

In order to find the return type of the function foo you would have to go via a function reference:

result_of<decltype(foo)& ()>::type == int

但这似乎有点扭曲,因为你可以直接写

But this seems a bit twisted as you could directly write

decltype(foo()) == int

这篇关于为什么不是std :: result_of&lt; int(int)&gt; :: type valid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆