简体   繁体   English

如何解释这个C ++类型?

[英]How to interpret this C++ type?

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): 今天我正在帮助跟踪不正确的代码片段( func是用int param声明的,但int*作为第二个param传递给std::thread构造函数):

#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: 当我尝试使用gcc 5.3.0编译它时,它打印了以下类型的错误消息:

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

Now I wonder how to interpret type passed as parameter to class std::result_of<> . 现在我想知道如何将作为参数传递的类型解释为class std::result_of<> It is similar to pointer to function (in this case void(*)(int) ), but with extra (int*) after star in brackets. 它类似于指向函数的指针(在本例中为void(*)(int) ),但在括号中的星号之后使用extra (int*) How to interpret this type? 如何解释这种类型?

This isn't as simple as it looks. 这并不像看起来那么简单。 std::result_of abuses the type system to smuggle in information about a function call so that it can figure out what the return type of that function call would be. std::result_of滥用类型系统来走私有关函数调用的信息,以便它可以找出该函数调用的返回类型。

The argument to std::result_of has the form Fty(T1, T2, ...) , where Fty is a callable type and T1 etc. are the types of the arguments that it is being called with. std::result_of的参数的形式为Fty(T1, T2, ...) ,其中Fty是可调用类型, T1等是调用它的参数类型。 Given that information, std::result_of has a nested type named type that is a synonym for the return type of calling a callable type with the signature Fty with arguments of the given types. 给定该信息, std::result_of具有名为type的嵌套类型,该类型是调用具有给定类型的参数的签名Fty的可调用类型的返回类型的同义词。 Phew, that's a mouthful. 哎呀,那是满口的。

So, in result_of<void (*(int*))(int)> there are two parts to the template argument. 因此,在result_of<void (*(int*))(int)>中,模板参数有两个部分。 The first part is void (*(int*)) , which is the callable type in question. 第一部分是void (*(int*)) ,这是有问题的可调用类型。 In this case it's a pointer to function that takes int* and returns void . 在这种情况下,它是一个指向函数的指针,它接受int*并返回void The second part is (int) , which is the type list for the proposed arguments. 第二部分是(int) ,它是建议参数的类型列表。

So what that is saying is that std::result_of is being instantiated with a function whose type is void (*(int*)) and with an argument list of (int) . 所以说的是std::result_of正在使用类型为void (*(int*))并且参数列表为(int)的函数进行实例化。 And that's the problem, as you indicated: you can't pass an argument of type int to a function that takes an argument of type int* . 这就是问题所在,正如您所指出的:您不能将类型为int的参数传递给采用int*类型参数的函数。

Aren't you glad you asked? 你问的不是很高兴吗? (Incidentally, this is pretty low-level template hackery that's not needed any more; decltype is a much cleaner way to figure out the return type of a function call). (顺便说一句,这是一个非常低级的模板hackery,不再需要了; decltype是一种更清晰的方法来计算函数调用的返回类型)。

void (*(int*))(int)

Is: 方法是:

a function that takes a single parameter of type int* as returns 一个函数,它接受int*类型的单个参数作为返回

a pointer to a function that takes a single parameter of type int and returns 指向函数的指针,该函数接受int类型的单个参数并返回

void


It is similar to the C/C++ standard library function signal : 它类似于C / C ++标准库函数信号

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). 它返回一个指向前一个信号处理程序的指针(与func参数的类型相同)。

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. 编辑:正如Pete Becker在评论中指出的那样 ,当与std::result_of ,它意味着不同的东西 ,但表达式本身仍然是我描述的类型, std::result_of只是以不同的方式解释它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM