问:输入臭名昭着的'旗帜'字段 [英] Q: Type'ing the infamous 'flags' field
问题描述
我写了很多驱动程序,并且不可避免地有一些硬件寄存器或
缓冲区描述符字段称为''flags''。标志通常定义为位b。
作为位位置。我想我可以得到一些编译时类型
在分配到一个标志字段时检查,但我不认为我可以。这是'/ b $ b我的想法:
typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag标志;
};
static const HwFlag flagA = 1<< 0;
static const HwFlag flagB = 1<< 1;
static const HwFlag flagC = 1<< 2;
但问题在于组合标志时:
struct HwThing dev;
dev.flags = flagA | flagB;
我想我需要一个演员回到HwFlag,因为flagA和B将提升为
某些整数类型不是吗?也就是说,我需要写:
dev.flags =(HwFlag)(flagA | flagB);
打败了我的意图。有什么建议吗?
谢谢。
-
- 马克 - >
-
在< Xn ********************* ***********@130.133.1.4> Mark A. Odell < OD ******* @ hotmail.com>写道:
我写了很多驱动程序,并且不可避免地有一些硬件寄存器或称为标志的缓冲区描述符字段。标志通常定义为位位置。我在想,在分配到一个标志字段时我可以得到一些编译时类型的检查,但我不认为我可以。
当你所有的操作数都有
整数类型时,你期望在C中进行什么样的类型检查?
这里''我在想什么:
typedef unsigned long HwFlag;
struct HwThing
{SomeType fieldOne;
SomeType fieldTwo;
HwFlag标志;
};
静态const HwFlag flagA = 1<< 0;
静态const HwFlag flagB = 1<< 1;
静态const HwFlag flagC = 1<< 2;
但问题是组合标志时:
struct HwThing dev;
dev.flags = flagA | FLAGB;
HwFlag HwFlag HwFlag
我想我需要回击HwFlag,因为flagA和B将提升为
某些整数类型不会他们?也就是说,我需要写:
因为flagA和flagB的类型是无符号长的,所以它们是有保证的。
不被触及整体促销。此外,它是
保证flagA |的类型flagB是无符号长的。
即使HwFlag是受整数促销影响的类型(未签名
char或unsigned short),以及flagA |的类型flagB成为int,你的
赋值仍然可以正常工作,因为int类型是有保证的
能够代表HwFlag类型的所有值。
dev.flags =(HwFlag)(flagA | flagB);
这相当于打败了我的意图。
您对C赋值运算符的工作方式有一点线索吗?
即使是flagA | flagB有一个不同于dev.flags的整数类型,
转换将由赋值运算符*自动*执行。
有什么建议吗?
吸引你的大脑。
Dan
-
Dan Pop
DESY Zeuthen,RZ集团
电子邮件: Da ***** @ ifh.de
目前在欧盟寻找工作
Da ***** @ cern.ch (Dan Pop)在新闻中写道:ci ********** @ sunnews.cern.ch:
dev.flags =(HwFlag)(flagA | flagB);
这相当于打败了我的意图。
你有丝毫的线索吗?关于C赋值运算符如何工作?
我猜不是。如果类型在编译时兼容,我认为它将rvalue的值赋给了左值的
位置。
即使flagA | flagB有一个不同于
dev.flags的整数类型,转换将由
赋值运算符*自动*执行。
'太棒了,但如果有人尝试,我想要编译时警告:
int myIllegalFlagValue = 75;
HwFlag dev;
dev.flags = myIllegalFlagValue; //请注意某种警告。
有什么建议吗?
吸引你的大脑。
像往常一样感谢Dan。有没有考虑过客户支持的工作? :-)
-
- 马克 - >
-
在< Xn ******************************** @ 130.133.1.4> Mark A. Odell < OD ******* @ hotmail.com>写道:
Da ***** @ cern.ch(Dan Pop)在新闻中写道:ci ********** @ sunnews.cern.ch:
即使是flagA | flagB有一个不同于
dev.flags的整数类型,转换将由
赋值运算符*自动*执行。
这很棒但是如果有人尝试,我想要编译时警告:
int myIllegalFlagValue = 75;
HwFlag dev;
dev.flags = myIllegalFlagValue; //请注意某种警告。
然后学习Pascal。正如我在上一篇文章中所说,C不是你想要的正确的
工具。
有什么建议吗?
吸引你的大脑。
像往常一样感谢Dan。有没有考虑过客户支持的工作? : - )
我目前的工作涉及大量的用户支持。例如
日期:星期四,2004年9月16日10:53:14 +0200
来自:Carsten Urbach< Ca ******** ****@physik.fu-berlin.de>
收件人:Dan Pop< Da ***** @ ifh.de>
主题:回复: PBS工作的问题
嗨Dan,
非常感谢您的快速帮助!
Carsten
Dan
-
Dan Pop
DESY Zeuthen,RZ group
电子邮件: Da*****@ifh.de
目前在欧盟寻找工作
I write a lot of drivers and there is inevitably some hardware register or
buffer descriptor field called ''flags''. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don''t think I can. Here''s
what I was thinking:
typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};
static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;
But the problem is when combining flags:
struct HwThing dev;
dev.flags = flagA | flagB;
I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won''t they? That is, I''d need to write:
dev.flags = (HwFlag) (flagA | flagB);
which rather defeats my intent. Any suggestions?
Thanks.
--
- Mark ->
--
In <Xn********************************@130.133.1.4> "Mark A. Odell" <od*******@hotmail.com> writes:
I write a lot of drivers and there is inevitably some hardware register or
buffer descriptor field called ''flags''. The flags are defined, typically,
as bit positions. I was thinking I could get some compile-time type
checking when assigning to a flag field but I don''t think I can.
What kind of type checking do you expect in C, when all the operands have
integral types?
Here''s what I was thinking:
typedef unsigned long HwFlag;
struct HwThing
{
SomeType fieldOne;
SomeType fieldTwo;
HwFlag flags;
};
static const HwFlag flagA = 1 << 0;
static const HwFlag flagB = 1 << 1;
static const HwFlag flagC = 1 << 2;
But the problem is when combining flags:
struct HwThing dev;
dev.flags = flagA | flagB;
HwFlag HwFlag HwFlag
I think I require a cast back to HwFlag since flagA and B will promote to
some integer type won''t they? That is, I''d need to write:
Since the type of flagA and flagB is unsigned long, they are guaranteed
not to be touched by the integral promotions. Furthermore, it is
guaranteed that the type of flagA | flagB is unsigned long.
Even if HwFlag were a type affected by the integral promotions (unsigned
char or unsigned short), and the type of flagA | flagB became int, your
assignment would still work as intended, because type int is guaranteed
to be able to represent all values of type HwFlag.
dev.flags = (HwFlag) (flagA | flagB);
which rather defeats my intent.
Do you have the slightest clue about how the C assignment operator works?
Even if flagA | flagB had a different integer type than dev.flags, the
conversion would be *automatically* performed by the assignment operator.
Any suggestions?
Engage your brain.
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
Da*****@cern.ch (Dan Pop) wrote in news:ci**********@sunnews.cern.ch:
dev.flags = (HwFlag) (flagA | flagB);
which rather defeats my intent.
Do you have the slightest clue about how the C assignment operator
works?
I guess not. I thought it assigned the value of rvalue to the
location of the lvalue if the types were compatible at compile time.
Even if flagA | flagB had a different integer type than
dev.flags, the conversion would be *automatically* performed by the
assignment operator.
That''s great but I want a compile time warning if someone attempts:
int myIllegalFlagValue = 75;
HwFlag dev;
dev.flags = myIllegalFlagValue; // Warning of some sort please.
Any suggestions?
Engage your brain.
Thanks Dan, as usual. Ever considered a job in customer support? :-)
--
- Mark ->
--
In <Xn********************************@130.133.1.4> "Mark A. Odell" <od*******@hotmail.com> writes:
Da*****@cern.ch (Dan Pop) wrote in news:ci**********@sunnews.cern.ch:Even if flagA | flagB had a different integer type than
dev.flags, the conversion would be *automatically* performed by the
assignment operator.
That''s great but I want a compile time warning if someone attempts:
int myIllegalFlagValue = 75;
HwFlag dev;
dev.flags = myIllegalFlagValue; // Warning of some sort please.
Then learn Pascal. As I said in my previous post, C is not the right
tool for what you want.
Any suggestions?
Engage your brain.
Thanks Dan, as usual. Ever considered a job in customer support? :-)
My current job involves a great deal of user support. E.g.
Date: Thu, 16 Sep 2004 10:53:14 +0200
From: Carsten Urbach <Ca************@physik.fu-berlin.de>
To: Dan Pop <Da*****@ifh.de>
Subject: Re: Problems with PBS jobs
Hi Dan,
Thanks a lot for your fast help!
Carsten
Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Currently looking for a job in the European Union
这篇关于问:输入臭名昭着的'旗帜'字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!