简体   繁体   English

这个typedef语句是什么意思?

[英]What does this typedef statement mean?

In a C++ reference page they provide some typedef examples and I'm trying to understand what they mean. 在C ++参考页面中,他们提供了一些typedef示例,我试图理解它们的含义。

// simple typedef
typedef unsigned long mylong;


// more complicated typedef
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];

So the simple typedef (the first declaration) I understand. 所以我理解的是简单的typedef(第一个声明)。

But what are they declaring with the second one (repeated below)? 但是他们用第二个宣告什么(下面重复)?

typedef int int_t, *intp_t, (&fp)(int, ulong), arr_t[10];

Particularly what does (&fp)(int, mylong) mean? 特别是(&fp)(int, mylong)是什么意思?

It's declaring several typedefs at once, just as you can declare several variables at once. 它一次声明了几个typedef,就像你可以一次声明几个变量一样。 They are all types based on int , but some are modified into compound types. 它们都是基于int类型,但有些被修改为复合类型。

Let's break it into separate declarations: 让我们把它分成单独的声明:

typedef int int_t;              // simple int
typedef int *intp_t;            // pointer to int
typedef int (&fp)(int, ulong);  // reference to function returning int
typedef int arr_t[10];          // array of 10 ints
typedef int int_t, *intp_t, (&fp)(int, mylong), arr_t[10];

is equivalent to: 相当于:

typedef int int_t;
typedef int *intp_t;
typedef int (&fp)(int, mylong);
typedef int arr_t[10];

There is actually a similar example in the C++11 standard: 在C ++ 11标准中实际上有一个类似的例子:

C++11 7.1.3 The typedef specifier C ++ 11 7.1.3 typedef说明符

A typedef -name does not introduce a new type the way a class declaration (9.1) or enum declaration does.Example: after typedef -name不会像class声明(9.1)或enum声明那样引入新类型。例如:after

 typedef int MILES , * KLICKSP ; 

the constructions 建筑

 MILES distance ; extern KLICKSP metricp ; 

are all correct declarations; 都是正确的声明; the type of distance is int that of metricp is “pointer to int .” —end example 距离的类型是int的of metricp是“指向int指针。” - 示例

If you have the cdecl command, you can use it to demystify these declarations. 如果您有cdecl命令,则可以使用它来揭开这些声明的神秘面纱。

cdecl> explain int (&fp)(int, char)
declare fp as reference to function (int, char) returning int
cdecl> explain int (*fp)(int, char)
declare fp as pointer to function (int, char) returning int

If you don't have cdecl , you should be able to install it in the usual way (eg on Debian-type systems, using sudo apt-get install cdecl ). 如果你没有cdecl ,你应该能够以通常的方式安装它(例如在Debian类型的系统上,使用sudo apt-get install cdecl )。

The (&fp)(int, mylong) part represents a reference to a function. (&fp)(int, mylong)部分表示对函数的引用。 It is not recommended that programmers use functions in typedef for the very reason you're asking this question. 由于您提出此问题的原因,不建议程序员使用typedef中的函数。 It confuses other people looking at the code. 它让看着代码的其他人感到困惑。

I'm guessing they use the typedef in something like this: 我猜他们在这样的事情中使用typedef

typedef unsigned long mylong; //for completeness
typedef int (&fp)(int, mylong);
int example(int param1, mylong param2);

int main() {
     fp fp_function = example;
     int x = fp_function(0, 1);
     return 0;
}

int example(int param1, mylong param2) {
     // does stuff here and returns reference
     int x = param1;
     return x;
}

Edited in accordance with Brian's comment: 按照Brian的评论编辑:

int(&name)(...) is a function reference called name (the function returns an int) int(&name)(...)是一个名为name函数引用 (该函数返回一个int)

int &name(...) is a function called name returning a reference to an int int &name(...)是一个名为name的函数, 返回对int的引用

A reference to a function which returns an int reference would look something like this: typedef int &(&fp)(int, mylong) (this compiles in a program, but the behaviour is untested). 对返回int引用的函数的引用如下所示: typedef int &(&fp)(int, mylong) (这在程序中编译,但行为未经测试)。

typedef is defining a new type for use in your code, like a shorthand. typedef定义了一个在代码中使用的新类型,比如速记。

typedef typename _MyBase::value_type value_type;
value_type v;
//use v

typename here is letting the compiler know that value_type is a type and not an object inside of _MyBase. 这里的typename让编译器知道value_type是一个类型,而不是_MyBase中的一个对象。

the :: is the scope of the type. ::是该类型的范围。 It is kind of like "is in" so value_type "is in" _MyBase. 它有点像“在”,所以value_type“在”_MyBase中。 or can also be thought of as contains. 或者也可以被认为是包含的。

Possible duplicate : C++ - meaning of a statement combining typedef and typename 可能重复: C ++ - 组合typedef和typename的语句的含义

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

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