typedef将object / enum声明为int [英] typedef declares object / enum to int

查看:98
本文介绍了typedef将object / enum声明为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这篇文章提出了两个问题,我已经在一个C源文件中说明了

(见下文),它清理了我的GNU编译器。


1.在K& R2的A8.9节中,它说存储类说明符

的声明是typedef的声明不声明对象。当我编译并运行我的示例代码时,

值为2。被展示。显然,typedef创建了枚举器

S1,S2和S3。这似乎与K& R2相矛盾。


2.下面的程序是为了模仿我在一个更大的项目中得到的Lint诊断而创建的。在该项目中,等效于fn(SYMBOL)

的行给出了警告


将enum''{...}''转换为int


我觉得很困惑。无论如何,枚举数都是int,而枚举

有一个整数值(根据K& R2,第A4.4节)。关于转换为int的

枚举器(int)的警告似乎毫无意义;关于被转换为int的枚举(整数类型)的警告

没有使得

感觉因为它是枚举器S3得到的

传递给fn()。我已经复制了Lint对下面警告的解释。


/ * typedef声明; Lint警告* /

#include< stdio.h>


typedef enum {S1,S2,S3} an_enum_t;

#define SYMBOL S3

typedef unsigned int Word;

void fn(Word w)

{

/ *故意清空* /

}


int main(无效)

{


fn(符号);

printf("%i \ n",SYMBOL);

返回0; / *成功* /

}


这是Lint对警告的描述:


641将枚举转换为int - 枚举类型用于上下文中,

需要计算,例如算术运算符的参数,或者是

与积分参数进行比较。如果您使用枚举的整数模型(+ fie),则会禁止此警告,但这样做会丢失一些有价值的

类型检查。中间政策是简单地关闭这个

警告。仍将捕获int到enum的赋值。


对于没有变量的无标记枚举,不会发出此警告。例如


enum {false,true};


这不能用作单独的类型。 PC-lint / FlexeLint识别这个

并将false和true视为算术常量。


-

Martin

解决方案

" Martin" < martin.o_brien @ [无垃圾邮件] which.net>写道:

1.在K& R2,A8.9节中,它说存储类说明符为/的声明为typedef的声明不声明对象。当我编译并运行我的示例代码时,值为2。被展示。显然,typedef创建了枚举器
S1,S2和S3。这似乎与K& R2相矛盾。


枚举值不是对象。对象是

存储的区域,但是枚举值只是值。

2.下面的程序是为了模拟我得到的Lint诊断而创建的更大的项目。 [...]




我从来没有对lint诊断法印象深刻。通常他们不会像你发现的那样在上下文中有很多意义。

-

"有一场圣战的事实并不意味着其中一方

不会吮吸 - 通常两者都做......

- Alexander viro


Martin写道:

这篇文章提出了两个问题,我在一个C源文件中说明了
(请参阅下文),在我的GNU编译器上清理编译。

1.在K& R2,A8.9节中,它说存储类说明符
为typedef的声明不声明对象"当我编译并运行我的示例代码时,值为2。被展示。显然,typedef创建了枚举器
S1,S2和S3。这似乎与K& R2相矛盾。


枚举常量正是如此:常量。

它们不是对象。

2.下面的程序是为了模拟一个Lint诊断我进入了一个更大的项目。在那个项目中,fn(SYMBOL)等效的行给出了警告

将enum''{...}''转换为int

发现令人费解。无论如何,枚举数都是int,而枚举
具有整数值(根据K& R2,第A4.4节)。关于转换为int的
枚举器(int)的警告似乎毫无意义;关于被转换为int的枚举(整数类型)的警告
并没有意义,因为它是枚举器S3传递给fn()。我已经复制了Lint对下面警告的解释。


澄清:

每个枚举类型与每个其他枚举类型或

整数类型不同,但与char或签名或未签名的

整数类型。 IOW:虽然标识符S3为你提供了一个类型为

int的常量,但是底层的枚举类型不一定是int。

干杯

迈克尔/ * typedef声明;皮棉警告* /
#include< stdio.h>

typedef enum {S1,S2,S3} an_enum_t;

#define SYMBOL S3

typedef unsigned int Word;
void fn(Word w)
{
/ *故意清空* /
}
int main (无效)
{/>
fn(符号);
printf("%i \ n",SYMBOL);
返回0; / *成功* /
}
这是Lint对警告的描述:

641将枚举转换为int - 枚举类型用于需要计算的上下文,例如算术运算符的参数,或者与积分参数进行比较。如果您使用枚举的整数模型(+ fie),则会禁止此警告,但这样做会丢失一些有价值的类型检查。中间政策是简单地关闭这个
警告。仍将捕获int到枚举的赋值。

对于没有变量的无标记枚举,不会发出此警告。例如

枚举{false,true};

这不能用作单独的类型。 PC-lint / FlexeLint识别这个
并将false和true视为算术常量。



-

电子邮件:我的是/ at / gmx / dot / de address。


Martin写道:

这篇文章提出两个问题,我已经在一个C源文件中说明
(见下文),它在我的GNU编译器上进行清理编译。

1.在K& R2中,A8.9节它说存储类的声明说明符
是typedef,不声明对象。当我编译并运行我的示例代码时,值为2。被展示。显然,typedef创建了枚举器
S1,S2和S3。这似乎与K& R2相矛盾。


否!

S1,S2和S3是*值*,类型为an_enum_t的对象可以有。

他们是*不是*他们自己的对象。

2.下面的程序是为了模拟一个Lint诊断而创建的。我参与了一个更大的项目。
在那个项目中, fn(SYMBOL)的等效行给出警告

将enum''{...}''转换为int

我觉得这很令人费解。无论如何,枚举数都是int,而枚举
具有整数值(根据K& R2,第A4.4节)。关于转换为int的
枚举器(int)的警告似乎毫无意义;
和关于枚举(整数类型)转换为int
的警告没有意义,因为它传递给fn()的枚举器S3。
我已经复制了Lint对下面警告的解释。
cat main.c
// typedef声明;皮棉警告

#include< stdio.h>


typedef enum {S1,S2,S3} an_enum_t;


#define符号S3


typedef unsigned int Word;


void fn(Word w){

//故意清空

}

int main(int argc,char * argv []){

fn(SYMBOL );

printf("%i \ n",SYMBOL);

返回0; //成功

}

gcc -Wall -std = c99 -pedantic -o main main.c
./main
2


这是Lint对警告的描述:

641将枚举转换为int - 枚举类型用于
需要的上下文中计算,例如算术运算符的参数或与积分参数进行比较。如果您使用枚举的整数模型(+ fie),则会禁止此警告,但这样做会丢失一些有价值的类型检查。中间政策是简单地关闭这个
警告。仍将捕获int到枚举的赋值。

对于没有变量的无标记枚举,不会发出此警告。例如

枚举{false,true};

这不能用作单独的类型。 PC-lint / FlexeLint识别这个
并将false和true视为算术常量。




所以你的抱怨是lint是挑剔的?


This post asks two questions, which I''ve illustrated in one C source file
(see below), which clean compiles on my GNU compiler.

1. In K&R2, Section A8.9 it says "Declarations whose storage class specifier
is typedef do not declare object." When I compile and run my sample code,
the value "2" is displayed. Clearly the typedef has created the enumerators
S1, S2, and S3. This appears to contradict K&R2.

2. The program below was created to emulate a Lint diagnostic I got in a
much larger project. In that project, the equivalent line to fn(SYMBOL)
gives the warning

Converting enum ''{...}'' to int

which I find puzzling. Enumerators are int anyway, whereas the enumeration
has an integral value (according to K&R2, Section A4.4). A warning about an
enumerator (int) being converted to an int seems meaningless; and a warning
about the enumeration (integral type) being converted to int doesn''t make
sense because it''s the enumerator S3 that gets
passed to fn(). I''ve reproduced Lint''s explanation of the warning below.

/* typedef declaration; Lint warning */
#include <stdio.h>

typedef enum { S1, S2, S3 } an_enum_t;

#define SYMBOL S3

typedef unsigned int Word;
void fn(Word w)
{
/* deliberately empty */
}

int main( void )
{

fn(SYMBOL);
printf("%i\n", SYMBOL);
return 0; /* success */
}

This is Lint''s description of the warning:

641 Converting enum to int -- An enumeration type was used in a context that
required a computation such as an argument to an arithmetic operator or was
compared with an integral argument. This warning will be suppressed if you
use the integer model of enumeration (+fie) but you will lose some valuable
type-checking in doing so. An intermediate policy is to simply turn off this
warning. Assignment of int to enum will still be caught.

This warning is not issued for a tagless enum without variables. For example

enum {false,true};

This cannot be used as a separate type. PC-lint/FlexeLint recognizes this
and treats false and true as arithmetic constants.

--
Martin

解决方案

"Martin" <martin.o_brien@[no-spam]which.net> writes:

1. In K&R2, Section A8.9 it says "Declarations whose storage class specifier
is typedef do not declare object." When I compile and run my sample code,
the value "2" is displayed. Clearly the typedef has created the enumerators
S1, S2, and S3. This appears to contradict K&R2.
Enumeration values are not objects. Objects are regions of
storage, but enumeration values are just values.
2. The program below was created to emulate a Lint diagnostic I got in a
much larger project. [...]



I''ve never been impressed with lint diagnostics. Often they
don''t make a whole lot of sense in context, as you''ve discovered.
--
"The fact that there is a holy war doesn''t mean that one of the sides
doesn''t suck - usually both do..."
--Alexander Viro


Martin wrote:

This post asks two questions, which I''ve illustrated in one C source file
(see below), which clean compiles on my GNU compiler.

1. In K&R2, Section A8.9 it says "Declarations whose storage class specifier
is typedef do not declare object." When I compile and run my sample code,
the value "2" is displayed. Clearly the typedef has created the enumerators
S1, S2, and S3. This appears to contradict K&R2.
Enumeration constants are exactly that: Constants.
They are not objects.
2. The program below was created to emulate a Lint diagnostic I got in a
much larger project. In that project, the equivalent line to fn(SYMBOL)
gives the warning

Converting enum ''{...}'' to int

which I find puzzling. Enumerators are int anyway, whereas the enumeration
has an integral value (according to K&R2, Section A4.4). A warning about an
enumerator (int) being converted to an int seems meaningless; and a warning
about the enumeration (integral type) being converted to int doesn''t make
sense because it''s the enumerator S3 that gets
passed to fn(). I''ve reproduced Lint''s explanation of the warning below.
To clarify:
Every enumerated type is distinct from every other enumerated or
integer type but is compatible to either char or a signed or unsigned
integer type. IOW: While the identifier S3 gives you a constant of type
int, the underlying enumerated type is not necessarily int.
Cheers
Michael /* typedef declaration; Lint warning */
#include <stdio.h>

typedef enum { S1, S2, S3 } an_enum_t;

#define SYMBOL S3

typedef unsigned int Word;
void fn(Word w)
{
/* deliberately empty */
}

int main( void )
{

fn(SYMBOL);
printf("%i\n", SYMBOL);
return 0; /* success */
}

This is Lint''s description of the warning:

641 Converting enum to int -- An enumeration type was used in a context that
required a computation such as an argument to an arithmetic operator or was
compared with an integral argument. This warning will be suppressed if you
use the integer model of enumeration (+fie) but you will lose some valuable
type-checking in doing so. An intermediate policy is to simply turn off this
warning. Assignment of int to enum will still be caught.

This warning is not issued for a tagless enum without variables. For example

enum {false,true};

This cannot be used as a separate type. PC-lint/FlexeLint recognizes this
and treats false and true as arithmetic constants.


--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Martin wrote:

This post asks two questions, which I''ve illustrated in one C source file
(see below), which clean compiles on my GNU compiler.

1. In K&R2, Section A8.9 it says "Declarations whose storage class specifier
is typedef do not declare object." When I compile and run my sample code,
the value "2" is displayed. Clearly the typedef has created the enumerators
S1, S2, and S3. This appears to contradict K&R2.
No!
S1, S2 and S3 are *values* that an object of type an_enum_t can have.
They are *not* themselves objects.
2. The program below was created to emulate a Lint diagnostic
[that] I got in a much larger project.
In that project, the equivalent line to fn(SYMBOL) gives the warning

Converting enum ''{...}'' to int

which I find puzzling. Enumerators are int anyway, whereas the enumeration
has an integral value (according to K&R2, Section A4.4). A warning about an
enumerator (int) being converted to an int seems meaningless;
and a warning about the enumeration (integral type) being converted to int
doesn''t make sense because it''s the enumerator S3 that gets passed to fn().
I''ve reproduced Lint''s explanation of the warning below. cat main.c // typedef declaration; Lint warning
#include <stdio.h>

typedef enum { S1, S2, S3 } an_enum_t;

#define SYMBOL S3

typedef unsigned int Word;

void fn(Word w) {
// deliberately empty
}

int main(int argc, char* argv[]) {
fn(SYMBOL);
printf("%i\n", SYMBOL);
return 0; // success
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main 2

This is Lint''s description of the warning:

641 Converting enum to int -- An enumeration type was used in a context that
required a computation such as an argument to an arithmetic operator or was
compared with an integral argument. This warning will be suppressed if you
use the integer model of enumeration (+fie) but you will lose some valuable
type-checking in doing so. An intermediate policy is to simply turn off this
warning. Assignment of int to enum will still be caught.

This warning is not issued for a tagless enum without variables. For example

enum {false,true};

This cannot be used as a separate type. PC-lint/FlexeLint recognizes this
and treats false and true as arithmetic constants.



So your complaint is that lint is nitpicking?


这篇关于typedef将object / enum声明为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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