const不是const [英] const is not const

查看:87
本文介绍了const不是const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一个原因,为什么const不是编译时常量?


通常的例子,它是不错的是数组声明:

const int N = 4;

float arr [N]


或者一个人可能想使用枚举作为索引:

enum index {a,b,c,d,N};

float arr [N]


当然两者都错了,解决方案是丑陋的定义

#define N 5


现在我找到了另一个例子,它能够很好地为
定义编译-time常量:

const char * const a [] = {" a"," bb"," ccc"};

const char * b [ ] = {a [1],a [0],a [2],a [2]};


这是一个错误,因为a [i]不是常数,是一个问题

因为我不知道任何优雅的方式来定义b [](让我们说a和b

是一个模块中的全局变量c代码)


(结构初始化的问题)


任何想法?

解决方案

Szabolcs Nagy写道:


有一个原因是const不能编译 - 时间常数?


通常的例子,数组声明很好:

const int N = 4;

float arr [N]


enum index {a,b,c,d,N };

float arr [N]


当然两者都错了,解决方案是一个丑陋的定义

#define N 5



lcc-win32 C编译器接受此作为扩展名。


我不确定我是否得到了所有案例好吧。上面的例子

编译没有问题。更有问题的是

常数变量的情况。例如,将在开关中使用。


现在我找到了另一个例子,能够将b $ b定义为编译时间常数:

const char * const a [] = {" a"," bb"," ccc"};

const char * b [] = {a [1],a [0],a [2],a [2]};


这是一个错误,因为a [i]不是常数,这是一个问题

因为我不知道任何优雅的方式来定义b [](让我们说a和b

是一个模块中的全局变量c代码)


(结构初始化的问题相同)


任何想法?



const char * const a [] = {" a"," bb"," ccc"};

const char ** b [] = {& a [1],& a [0],& a [2],& a [2]};


虽然有效但很丑......


-

jacob navia

jacob at jacob point remcomp point fr

logiciels / informa tique
http://www.cs.virginia.edu/ ~lcc-win32


Szabolcs Nagy写道:


有一个原因是为什么const是不是编译时常量吗?


通常的例子,数组声明很好:

const int N = 4;

float arr [N]



在C99中没问题,但在C89 / 90中没有。


或者一个人可能想使用枚举的枚举:

enum index {a,b,c,d,N};

float arr [ N]


当然两个都错了,解决方案是一个丑陋的定义

#define N 5



即使在原始版本中,带枚举的版本也可以。 ANSI C(C89 / 90)。我不知道

为什么你称它为错误。


现在我找到另一个例子,它会很好能够
定义编译时常量:

const char * const a [] = {" a"," bb"," ccc"};

const char * b [] = {a [1],a [0],a [2],a [2]};


this是一个错误,因为[i]不是常数,这是一个问题

,因为我不知道任何优雅的方式来定义b [](让我们说a和b

是一个模块中的全局变量c代码)


(结构初始化的问题)



对于非静态对象,在C99中也可以。


任何想法?



好​​吧,这可能只是为了让这些语言的原始版本更简单一些。对编译器实现者来说更简单。


-

祝你好运,

Andrey Tarasevich


Szabolcs Nagy在11/07/07 09:01写道:


有一个原因,为什么const不是编译时常量?



如果有人回答否,你会相信它吗?


通常的例子,它是不错的是数组声明:

const int N = 4;

float arr [N]



想一想这一段时间:


extern const int N; / *在别处定义* /

float arr [N]; / * ......但是有什么价值? * /


或者一个人可能想使用枚举的枚举:

枚举索引{a,b,c,d,N} ;

float arr [N]



这很好(添加分号后)。


当然两个都错了



不,只有第一个失败。


并且解决方案是一个丑陋的定义

#define N 5


现在我找到另一个例子,能够

定义编译时常量:

const char * const a [] = {" a"," bb"," ccc"};

const char * b [] = {a [1],a [0],a [2],a [2]};


这是一个错误因为a [i]不是常数,这是一个问题

因为我不知道任何优雅的方式来定义b [](让我们说a和b

是全局的在一个模块c代码)



你喜欢&quo吨;优雅"到有效?如果是这样,

不使用


const char astring [] =" a" ;;

const char bstring [] =" bb";

const char cstring [] =" ccc" ;;

const char * const a [] = {astring,bstring,cstring};

const char * b [] = {

bstring,astring,cstring,cstring};


(相同结构初始化的问题)



相同的解决方案,除非它不符合你的优雅的b $ b标准。


任何想法?



了解您的工具如何运作。然后使用它们,如果它们的工作足够好,或者放弃它们并选择

其他工具,如果没有。不要浪费时间抱怨你的

十字螺丝刀没有转动Torx螺栓。


-
呃********* @ sun.com


is there a reason why const is not compile-time constant?

the usual example, where it''d be nice is array declaration:
const int N = 4;
float arr[N]

or one may want to use an enum for indices:
enum index {a,b,c,d,N};
float arr[N]

of course both are wrong and the solution is an ugly define
#define N 5

now i found another example where it would be nice to be able to
define compile-time constants:
const char * const a[] = {"a", "bb", "ccc"};
const char *b[] = {a[1], a[0], a[2], a[2]};

this is an error because a[i] is not constant, which is a problem
since i don''t know any elegant way to define b[] (let''s say a and b
are globals in a one module c code)

(same problem with struct initialization)

any ideas?

解决方案

Szabolcs Nagy wrote:

is there a reason why const is not compile-time constant?

the usual example, where it''d be nice is array declaration:
const int N = 4;
float arr[N]

or one may want to use an enum for indices:
enum index {a,b,c,d,N};
float arr[N]

of course both are wrong and the solution is an ugly define
#define N 5

The lcc-win32 C compiler accepts this as an extension.

I am not sure I got all the cases right though. The above example
compiles without problems. More problematic are the cases where the
constant "variable" would be used in a switch for instance.

now i found another example where it would be nice to be able to
define compile-time constants:
const char * const a[] = {"a", "bb", "ccc"};
const char *b[] = {a[1], a[0], a[2], a[2]};

this is an error because a[i] is not constant, which is a problem
since i don''t know any elegant way to define b[] (let''s say a and b
are globals in a one module c code)

(same problem with struct initialization)

any ideas?



const char * const a[] = {"a", "bb", "ccc"};
const char **b[] = {&a[1], &a[0], &a[2], &a[2]};

That works but is ugly...

--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32


Szabolcs Nagy wrote:

is there a reason why const is not compile-time constant?

the usual example, where it''d be nice is array declaration:
const int N = 4;
float arr[N]

It is OK in C99, but not OK in C89/90.

or one may want to use an enum for indices:
enum index {a,b,c,d,N};
float arr[N]

of course both are wrong and the solution is an ugly define
#define N 5

The version with enum is OK even in the "original" ANSI C (C89/90). I don''t know
why you call it "wrong".

now i found another example where it would be nice to be able to
define compile-time constants:
const char * const a[] = {"a", "bb", "ccc"};
const char *b[] = {a[1], a[0], a[2], a[2]};

this is an error because a[i] is not constant, which is a problem
since i don''t know any elegant way to define b[] (let''s say a and b
are globals in a one module c code)

(same problem with struct initialization)

Also OK in C99 for non-static objects.

any ideas?

Well, it was probably done this way just to make things a bit simpler in the
original revisions of the language. Simpler to the compiler implementers that is.

--
Best regards,
Andrey Tarasevich


Szabolcs Nagy wrote On 11/07/07 09:01,:

is there a reason why const is not compile-time constant?

If someone answered "No," would you believe it?

the usual example, where it''d be nice is array declaration:
const int N = 4;
float arr[N]

Think about this one for a few moments:

extern const int N; /* defined elsewhere */
float arr[N]; /* ... but with what value? */

or one may want to use an enum for indices:
enum index {a,b,c,d,N};
float arr[N]

This is fine (after adding a semicolon).

of course both are wrong

No, only the first fails.

and the solution is an ugly define
#define N 5

now i found another example where it would be nice to be able to
define compile-time constants:
const char * const a[] = {"a", "bb", "ccc"};
const char *b[] = {a[1], a[0], a[2], a[2]};

this is an error because a[i] is not constant, which is a problem
since i don''t know any elegant way to define b[] (let''s say a and b
are globals in a one module c code)

Do you prefer "elegant" to "effective?" If so,
do not use

const char astring[] = "a";
const char bstring[] = "bb";
const char cstring[] = "ccc";
const char * const a[] = { astring, bstring, cstring };
const char * b[] = {
bstring, astring, cstring, cstring };

(same problem with struct initialization)

Same solution, unless it fails to meet your
standards of elegance.

any ideas?

Learn how your tools work. Then use them if they work
well enough for your purposes, or abandon them and choose
other tools if not. Don''t waste time whining that your
Phillips screwdriver doesn''t turn Torx bolts.

--
Er*********@sun.com


这篇关于const不是const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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