这个typedef语句是什么意思?

时间:2021-10-23 22:26:02

In a C++ reference page they provide some typedef examples and I'm trying to understand what they mean.

在c++参考页面中,它们提供了一些类型定义示例,我正在尝试理解它们的含义。

// 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)是什么意思?

5 个解决方案

#1


96  

It's declaring several typedefs at once, just as you can declare several variables at once. They are all types based on int, but some are modified into compound types.

它同时声明几个typedef,就像您可以同时声明几个变量一样。它们都是基于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

#2


41  

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

A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.Example: after

typedef-name不像类声明(9.1)或enum声明那样引入新类型。例子:在

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,而metricp的类型是“指向int.”-结束示例

#3


31  

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 (e.g. on Debian-type systems, using sudo apt-get install cdecl).

如果没有cdecl,您应该能够以通常的方式安装它(例如,在debian类型的系统上,使用sudo apt-get install cdecl)。

#4


0  

The (&fp)(int, mylong) part represents a reference to a function. It is not recommended that programmers use functions in typedef for the very reason you're asking this question. It confuses other people looking at the code.

(&fp)(int, mylong)部分表示对函数的引用。不建议程序员使用typedef中的函数,原因正是您提出这个问题的原因。它使其他查看代码的人感到困惑。

I'm guessing they use the typedef in something like this:

我猜他们用的是这样的类型定义:

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)(它在程序中编译,但是行为是未经测试的)。

#5


-7  

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. or can also be thought of as contains.

::为类型范围。它有点像"is in" so value_type "is in" _MyBase。或者也可以认为是包含的。

Possible duplicate : C++ - meaning of a statement combining typedef and typename

可能的重复:c++——结合了类型定义和笔名的语句的意思

#1


96  

It's declaring several typedefs at once, just as you can declare several variables at once. They are all types based on int, but some are modified into compound types.

它同时声明几个typedef,就像您可以同时声明几个变量一样。它们都是基于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

#2


41  

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

A typedef-name does not introduce a new type the way a class declaration (9.1) or enum declaration does.Example: after

typedef-name不像类声明(9.1)或enum声明那样引入新类型。例子:在

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,而metricp的类型是“指向int.”-结束示例

#3


31  

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 (e.g. on Debian-type systems, using sudo apt-get install cdecl).

如果没有cdecl,您应该能够以通常的方式安装它(例如,在debian类型的系统上,使用sudo apt-get install cdecl)。

#4


0  

The (&fp)(int, mylong) part represents a reference to a function. It is not recommended that programmers use functions in typedef for the very reason you're asking this question. It confuses other people looking at the code.

(&fp)(int, mylong)部分表示对函数的引用。不建议程序员使用typedef中的函数,原因正是您提出这个问题的原因。它使其他查看代码的人感到困惑。

I'm guessing they use the typedef in something like this:

我猜他们用的是这样的类型定义:

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)(它在程序中编译,但是行为是未经测试的)。

#5


-7  

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. or can also be thought of as contains.

::为类型范围。它有点像"is in" so value_type "is in" _MyBase。或者也可以认为是包含的。

Possible duplicate : C++ - meaning of a statement combining typedef and typename

可能的重复:c++——结合了类型定义和笔名的语句的意思