枚举+长 [英] enum + long
问题描述
如果我理解得很清楚,只有保证最多只能保留一个
int(6.7.2.2-2)。所以我想知道:
如何在枚举中存储长篇?
enum {p2_31 = 1L<< 31};
//热潮
如何定义常量地址的同义词?
enum { p =(ptrdiff_t)& address_of_an_unknown_variable};
// boom,ptrdiff_t通常是long类型。
重要的方面是constness,因为我会喜欢使用新的
标识符来初始化静态数据。这主要是第二种情况,即使两者都是等价的,我感兴趣的是。
注意我不能使用预处理器,因为我的问题在
cpp阶段。这就是为什么当我需要
来获取其地址时我不知道变量标识符(但是当我可以定义枚举时我就知道了)。
谢谢。
a +,ld。
Laurent Deniau在03/17/06 12:25写道:如果我理解得很清楚,枚举只是保证最多可以保留一个
int(6.7.2.2- 2)。所以我想知道:
如何在枚举中存储长篇?
枚举{p2_31 = 1L<< 31};
//热潮
枚举常量*是* int,绝不是长或短
或无符号整数或者还要别的吗。因此,定义一个枚举常量并不比INT_MIN大于INT_MIN或者大于INT_MAX可能不是
。
虽然枚举常量是整数,但枚举类型
本身可以是编译器选择的任何整数形式
(但它必须选择一个可以代表所有的类型/>
相关常量的值)。因此,它只是
可能在
enum {A,B,C} x;
....'x''实际上可能很长,你可能实际上是/ b $ b能够在其中存储一个长值:`x = LONG_MAX''。
但是,没有可移植的方法可以准确地发现
编译器为x选择的类型,因此没有办法确定赋值是否可以按预期工作。
如何定义常量地址的同义词?
枚举{p =(ptrdiff_t)& address_of_an_unknown_variable};
// boom,ptrdiff_t一般类型很长。
指定枚举常量的值时,必须使用编译时常量表达式来使用
。地址不是
编译时常量。
重要的方面是constness,因为我想使用新的
标识符来初始化静态数据。这主要是第二个让我感兴趣的案例,即使两者都是等价的。
请注意,由于我的问题在
cpp阶段之后上升,我无法使用预处理器。这就是为什么当我需要
取其地址时我不知道变量标识符(但我知道它可以定义枚举)。
"当您定义枚举时必须是当你写
源代码时,反过来必须在预处理器之前运行
,所以我不明白为什么预处理器不能做你想要的
。你到底想要做什么?
-
Er ********* @ sun.com
2006年3月17日星期五17:25,Laurent Deniau认为(在
< dv ********** @ sunnews.cern.ch>):
如果我理解得很好,枚举最多只能保留一个int(6.7.2.2-2)。所以我想知道:
如何在枚举中存储长篇内容?
你不能......
enum {p2_31 = 1L<< 31};
//繁荣
如何定义常量地址的同义词?
我不确定我明白这一点,但现在已经很晚了......
enum {p =(ptrdiff_t)& ; address_of_an_unknown_variable};
// boom,ptrdiff_t通常是long类型。
重要的方面是constness,因为我想使用
新标识符来初始化静态数据。主要是第二个案例
我感兴趣,
为什么你坚持使用`enum`来表达对我来说似乎是
像一个常量指针?为什么这些数组,由'enum`
索引不会(如果你仍然坚持使用`enum`)?
即使两者都是等价的。
他们看起来不像我。 `ptrdiff_t`不能保证定义为`long`
。
请注意,由于我的问题在cpp阶段之后上升,我无法使用预处理器。这就是为什么当我需要获取其地址时我不知道变量标识符的原因(但我知道它可以定义枚举)。
谢谢。
a +,ld。
-
BR,弗拉基米尔
最接近完美的人是他们填写
职位申请时。
Eric Sosman写道:
Laurent Deniau在03/17/06 12:25写道:
如果我理解得很清楚,只有保证在
大多数是一个int(6.7.2.2-2)。所以我想知道:
如何在枚举中存储长篇?
枚举{p2_31 = 1L<< 31}; // boom
枚举常量*是* int,绝不是long或short或unsigned
int或其他任何东西。因此,无法定义值小于INT_MIN或大于INT_MAX的
枚举常量。
是。这就是我所说的。上面的例子是目标
(根据我真正关心的下一点),而不是这样做的方法。
如何定义常量地址的同义词?
枚举{p =(ptrdiff_t)& address_of_an_unknown_variable}; // boom,
ptrdiff_t通常是long类型。
指定枚举常量的值时,必须使用
编译时常量表达式。地址不是编译时
常量。
枚举需要一个整数常量表达式和一个
变量的地址静态存储持续时间是恒定的(6.6-9)。但似乎
转换为ptrdiff_t打破了导致
无效表达式的常量(即要么不是常量,要么不是整数)。
请注意,由于我的问题在cpp阶段之后上升,我无法使用预处理器。这就是为什么当我需要获取其地址时我不知道变量
标识符(但是当我确定枚举时我就知道了。)
"当您定义枚举时必须是当你编写源代码时,而
反过来必须是在预处理器运行之前,所以我不明白为什么预处理器不能做你想做的事情。究竟是什么,你想要做什么?
这里解释一下有点复杂并且需要显示代码示例
介绍大约一百个宏操作令牌列表和
复杂代码生成。
同时我决定完全重新设计部分代码到
避免这个问题。但是我不得不使用工会而且我不喜欢(更少
优雅)。
a +,ld。
If I understand well, an enumeration is only garantee to hold at most an
int (6.7.2.2-2). So I would like to know:
how to store a long in an enum?
enum { p2_31 = 1L << 31 };
// boom
how to define a synonym of a constant address?
enum { p = (ptrdiff_t)&address_of_an_unknown_variable };
// boom, ptrdiff_t is generally of type long.
The important aspect is the constness since I would like to use the new
identifier to initialize static data. It is mainly the second case which
interests me, even if both are equivalent.
Note that I cannot use the preprocessor since my problem rises after the
cpp phase. This is why I don''t know the variable identifier when I need
to take its address (but I know it when I can define the enum).
Thanks.
a+, ld.
Laurent Deniau wrote On 03/17/06 12:25,:If I understand well, an enumeration is only garantee to hold at most an
int (6.7.2.2-2). So I would like to know:
how to store a long in an enum?
enum { p2_31 = 1L << 31 };
// boom
An enum constant *is* an int, never a long or a short
or an unsigned int or anything else. Therefore, it is not
possible to define an enum constant whose value is less
than INT_MIN or greater than INT_MAX.
Although the enum constants are ints, the enum type
itself can be any kind of integer the compiler chooses
(but it must choose a type that can represent all the
values of the associated constants). Thus, it is just
possible that in
enum { A, B, C } x;
.... `x'' might actually be a long, and you might actually
be able to store a long value in it: `x = LONG_MAX''.
However, there is no portable way to discover exactly
what type the compiler selected for `x'', hence no way to
predict whether the assignment will work as desired.
how to define a synonym of a constant address?
enum { p = (ptrdiff_t)&address_of_an_unknown_variable };
// boom, ptrdiff_t is generally of type long.
When you specify a value for an enum constant, you must
use a compile-time constant expression. Addresses are not
compile-time constants.
The important aspect is the constness since I would like to use the new
identifier to initialize static data. It is mainly the second case which
interests me, even if both are equivalent.
Note that I cannot use the preprocessor since my problem rises after the
cpp phase. This is why I don''t know the variable identifier when I need
to take its address (but I know it when I can define the enum).
"When you define the enum" must be "when you write the
source code," which in turn must be "before the preprocessor
runs," so I do not understand why the preprocessor cannot do
what you want. What, exactly, are you trying to do?
--
Er*********@sun.com
On Friday 17 March 2006 17:25, Laurent Deniau opined (in
<dv**********@sunnews.cern.ch>):
If I understand well, an enumeration is only garantee to hold at most
an int (6.7.2.2-2). So I would like to know:
how to store a long in an enum?
You can''t...
enum { p2_31 = 1L << 31 };
// boom
how to define a synonym of a constant address?
I''m not sure I understand this, but it''s late in the day...
enum { p = (ptrdiff_t)&address_of_an_unknown_variable };
// boom, ptrdiff_t is generally of type long.
The important aspect is the constness since I would like to use the
new identifier to initialize static data. It is mainly the second case
which interests me,
Why are you insisting on using `enum` for something that, to me, seems
like a constant pointer? Why an array of these, indexed by an `enum`
won''t do (if you still insist on using `enum`)?
even if both are equivalent.
They don''t look equivalent to me. `ptrdiff_t` is not guaranteed to be
defined as `long`.
Note that I cannot use the preprocessor since my problem rises after
the cpp phase. This is why I don''t know the variable identifier when I
need to take its address (but I know it when I can define the enum).
Thanks.
a+, ld.
--
BR, Vladimir
The closest to perfection a person ever comes is when they fill out a
job application.
Eric Sosman wrote:
Laurent Deniau wrote On 03/17/06 12:25,:If I understand well, an enumeration is only garantee to hold at
most an int (6.7.2.2-2). So I would like to know:
how to store a long in an enum?
enum { p2_31 = 1L << 31 }; // boom
An enum constant *is* an int, never a long or a short or an unsigned
int or anything else. Therefore, it is not possible to define an
enum constant whose value is less than INT_MIN or greater than
INT_MAX.
Yes. This is what I was saying. The example above was the goal
(according to the next point which is my real care), not the way to do it.
how to define a synonym of a constant address?
enum { p = (ptrdiff_t)&address_of_an_unknown_variable }; // boom,
ptrdiff_t is generally of type long.
When you specify a value for an enum constant, you must use a
compile-time constant expression. Addresses are not compile-time
constants.
An enum requires an integral constant expression and the address of a
variable with static storage duration is constant (6.6-9). But it seems
that the conversion to ptrdiff_t break the constness leading to an
invalid expression (i.e. either not constant, either not integral).
Note that I cannot use the preprocessor since my problem rises
after the cpp phase. This is why I don''t know the variable
identifier when I need to take its address (but I know it when I
can define the enum).
"When you define the enum" must be "when you write the source code,"
which in turn must be "before the preprocessor runs," so I do not
understand why the preprocessor cannot do what you want. What,
exactly, are you trying to do?
A bit complex to explain here and showing a code sample would need to
introduce about a hundred of macros manipulating list of tokens and
complex code generation.
In the mean time I decided to completely redesign part of the code to
avoid this problem. But I had to use a union and I don''t like to (less
elegant).
a+, ld.
这篇关于枚举+长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!