如何解释这个C ++类型? [英] How to interpret this C++ type?

查看:78
本文介绍了如何解释这个C ++类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我正在帮助处理以下不正确的代码片段( func 被声明为 int param,但是 int * 作为第二个参数传递给 std :: thread 构造函数):

  #include< thread> 

void func(int);
int * ptr;

void start()
{
std :: thread t = std :: thread(func,ptr);
}

当我试着用gcc 5.3.0编译它时,它打印出错信息使用以下类型:

  class std :: result_of< void(*(int *))(int)> 

现在我想知道如何将参数类型传递给 class std ::的result_of<> 。它类似于指向函数的指针(在这种情况下 void(*)(int)),但带有额外的(int *)括号后面的星号。如何解释这种类型?

解决方案

  void(*(int *))(int )

是:

接受一个类型为 int * 作为返回值的单个参数

指向一个函数的指针,该函数接受一个类型的单个参数 int 并返回



void






它类似于C / C ++标准库函数 signal

  void(* signal(int sig,void (* FUNC)(INT)))(int);在

它返回一个指向先前信号处理程序的指针(它与 func 参数)。



编辑:如 Pete Becker在评论中指出,当与 std :: result_of ,它意味着不同的东西,但表达式类型本身仍然是我描述的类型, std :: result_of 的解释不同。


Today I was helping with following incorrect code piece (func was declared with int param, but int* was passed as second param to std::thread constructor):

#include <thread>

void func(int);
int* ptr;

void start()
{
    std::thread t = std::thread(func, ptr);
}

When I tried to compile this with gcc 5.3.0, it printed error message with following type:

class std::result_of<void (*(int*))(int)>

Now I wonder how to interpret type passed as parameter to class std::result_of<>. It is similar to pointer to function (in this case void(*)(int)), but with extra (int*) after star in brackets. How to interpret this type?

解决方案

void (*(int*))(int)

Is:

a function that takes a single parameter of type int* as returns

a pointer to a function that takes a single parameter of type int and returns

void


It is similar to the C/C++ standard library function signal:

void (*signal(int sig, void (*func)(int)))(int);

which returns a pointer to a previous signal handler (which is of the same type as the func parameter).

EDIT: As Pete Becker pointed out in comment, when used with std::result_of, it means something different, but type of expression itself is still the type I described, std::result_of just interprets it differently.

这篇关于如何解释这个C ++类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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