更好的解决方案? De-Const'ing变量 [英] A better solution? De-Const'ing a variable

查看:41
本文介绍了更好的解决方案? De-Const'ing变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,我希望它能够抗拒未来。我有一堆

变量设置为运行时,一旦设置,应该充当

常量。我不想#define他们,因为他们的值在执行初始化例程时确定了
,所以我把它们定为

(我会相反,让开发人员很难意外地覆盖这些值。)

问题是,它们是通过返回函数来设置运行时间的。

调用。现在我的解决方案很难看,并要求我将.c文件转换为

..cpp文件。关于如何以更好的方式做到这一点的任何建议?我不能通过谷歌或我的参考书来找到它。


基本上我正在尝试为一个语句创建一个变量非const 。


ex:

in .h

extern const type_id CHAR_TYPE;

$ b .cpp中的$ b我宁愿是.c

const type_id CHAR_TYPE;

/*...*/

void mylib_init()

{

/ *注意寻址和令人痛苦的操作,const_cast requries指针

工作...... * /

*(const_cast< type_id *>(& CHAR_TYPE))= ctypeless_register(sizeof(char),

NULL,NULL," char");

}

所以,如果你像我一样,你会看到我的解决方案存在大量问题。任何

建议的修复是(a)不那么难看,(b)不需要使用

C ++,并且(c)确保那些人使用代码获取警告/错误如果他们

尝试更改这些变量的值。


谢谢,

-Jim Stapleton

I have some code, and I want to make it future-resistant. I have a bunch of
variables that are set up run-time, and once set up, should act as
constants. I don''t want to #define them, because their values are determined
during the execution of the initialization routines, so I made them const
(I''d rather making it hard for a dev to accidentally overwrite the values).
The problem is, they are being set run-time by the return of a function
call. Right now my solution is ugly, and required me to turn a .c file to a
..cpp file. Any suggestions on how to do this in a better manner? I couldn''t
find it via google or my reference books.

Basically I''m trying to make a variable non-const for one statement.

ex:
in the .h
extern const type_id CHAR_TYPE;

in the .cpp which I''d rather be a .c
const type_id CHAR_TYPE;
/*...*/
void mylib_init()
{
/*note the addressing and deadressing ops, const_cast requries pointers
to work...*/
*(const_cast<type_id*>(&CHAR_TYPE)) = ctypeless_register(sizeof(char),
NULL, NULL, "char");
}
so, if you are like me, you see heaploads of problems with my solution. Any
suggestions for a fix that is (a) less ugly, (b) doesn''t require the use of
C++, and (c) makes sure people who use the code get warnings/errors if they
try to change the values of these variables.

Thanks,
-Jim Stapleton

推荐答案

S James S Stapleton写道:
S James S Stapleton wrote:

我有一些代码,我想让它具有抗衰老能力。我有一堆

变量设置为运行时,一旦设置,应该充当

常量。我不想#define他们,因为他们的值在执行初始化例程时确定了
,所以我把它们定为

(我会相反,让开发人员很难意外地覆盖这些值。)

问题是,它们是通过返回函数来设置运行时间的。

调用。现在我的解决方案很难看,并要求我将.c文件转换为

.cpp文件。关于如何以更好的方式做到这一点的任何建议?我不能通过谷歌或我的参考书来找到它。


基本上我正在尝试为一个语句创建一个变量非const 。

[...]
I have some code, and I want to make it future-resistant. I have a bunch of
variables that are set up run-time, and once set up, should act as
constants. I don''t want to #define them, because their values are determined
during the execution of the initialization routines, so I made them const
(I''d rather making it hard for a dev to accidentally overwrite the values).
The problem is, they are being set run-time by the return of a function
call. Right now my solution is ugly, and required me to turn a .c file to a
.cpp file. Any suggestions on how to do this in a better manner? I couldn''t
find it via google or my reference books.

Basically I''m trying to make a variable non-const for one statement.
[...]



我见过用于最终的两种方法全局变量:


/ * foo.h * /

int getGlobal(void);

#define global getGlobal()


/ * foo.c * /

#include" foo.h"

static int actualGlobal;

void init_foo(void){

actualGlobal = whatever;

}

int getGlobal(void){

返回actualGlobal;

}


另一个可能是头发效率更高,但也是一个触摸

更少防弹:


/ * foo.h * /

extern const int * const globalPtr;

#define global (* globalPtr)


/ * foo.c * /

#include" foo.h"

static int actualGlobal ;

const int * const globalPtr =& actualGlobal;

void init_foo(void){

actualGlobal = whatever;

}


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


>
>

我见过用于最终的两种方法全局变量:


/ * foo.h * /

int getGlobal(void);

#define global getGlobal()


/ * foo.c * /

#include" foo.h"

static int actualGlobal;

void init_foo(void){

actualGlobal = whatever;

}

int getGlobal(void){

返回actualGlobal;

}
Two approaches I''ve seen used for "final" global variables:

/* foo.h */
int getGlobal(void);
#define global getGlobal()

/* foo.c */
#include "foo.h"
static int actualGlobal;
void init_foo(void) {
actualGlobal = whatever;
}
int getGlobal(void) {
return actualGlobal;
}



啊,这是一个很好的解决方案。

Ah, that is a nice solution.


>

另一个可能是头发更有效率,但也是一个触摸

少防弹:
>
The other may be a hair more efficient, but is also a touch
less bullet-proof:



实际上,我不确定效率部分。如果库没有动态加载
,那么这不是通过编译器自动内联的吗?如果

编译器不够智能,你还能添加内联吗?那将是b / b
使其与直接变量访问一样高效。但是,对于动态

链接,另一种方法会更有效(遵循指针+

变量访问而不是调用函数的开销)。


谢谢,

-Jim Stapleton

actually, I''m not sure on the efficiency part. If the library is not
dynamically loaded, wouldn''t this be auto-inlined via the compiler? And if
the compiler isn''t smart enough, could you add an inline anyway? That would
make this as efficient as a straight variable access. However, for dynamic
linkage, the other method would be more efficient (follow a pointer +
variable access instead of the overhead of calling a function).

Thanks,
-Jim Stapleton




S James S Stapleton写道:


....在新闻组中*和*在电子邮件中;在新闻组* xor *
电子邮件中的
更可取。

S James S Stapleton wrote:

.... in the newsgroup *and* in E-mail; in the newsgroup *xor*
in E-mail would have been preferable.

> Eric Sosman写道
[...]
另一个可能是头发更有效率,但也是一个触摸
少防弹:
>Eric Sosman wrote
[...]
The other may be a hair more efficient, but is also a touch
less bullet-proof:


实际上是
,我对效率部分不太确定。如果库没有动态加载
,那么这不是通过编译器自动内联的吗?


actually, I''m not sure on the efficiency part. If the library is not
dynamically loaded, wouldn''t this be auto-inlined via the compiler?



可能不是,因为包含private>
global的模块及其accessor函数将单独编译

来自使用它的模块。 (如果它们被编译在一起,那么全局的隐私将会消失......)尽管如此,一个足够的

智能链接器可能会完成这项任务。 />

Probably not, because the module containing the "private"
global and its accessor function would be compiled separately
from the modules that used it. (If they were compiled together,
the "privacy" of the global would disappear ...) A sufficiently
smart linker might do the job, though.




如果编译器不够智能,你还能添加内联吗?

会使其与直接变量访问一样高效。
And
if the compiler isn''t smart enough, could you add an inline anyway? That
would make this as efficient as a straight variable access.



你如何隐藏私人?全局但仍然可以使内联函数可见
?我想你可以制作

全球函数内部的局部静态并在初次使用时将其初始化

,但我不知道任何编译器实际上可以使用本地静态变量实现
内联函数。他们需要

以某种方式将函数的所有副本中的静态合并到

中只需一个 - 它可以通过将静态设置为extern和
给它一些搞定的名字,并以某种方式将它分配给

,但是......

How would you hide the "private" global but still make it
visible to an inlined function? I suppose you could make the
"global" a local static inside the function and initialize it
on first use, but I don''t know of any compilers that can actually
in-line functions with local static variables. They''d need to
somehow merge the statics from all copies of the function into
just one -- it could be done by making the static an extern and
giving it some kind of screwball name and somehow getting it to
be allocated, but ...


但是,对于

动态链接,另一种方法会更有效(遵循一个

指针+变量访问而不是调用函数的开销)。
However, for
dynamic linkage, the other method would be more efficient (follow a
pointer + variable access instead of the overhead of calling a function).



关于效率的推理是出了名的狡猾。唯一可靠的方法是衡量 - 而且在公园里没有步行

也要有意义。此外,当你改变编译器或标志,或者b
平台时,甚至当你制作一个不相关的平台时,测量结果会有不同的变化。改变那个移动

从一个缓存行到另一个缓存行的某些数据......无论如何,问题

的效率必然是特定于实现的,并且不是

关于编程语言本身的问题。


最佳投注:(1)不要担心某事物的效率

这样直到除非你有充分的理由相信

这是一个问题,并且(2)如果访问这些全局并且实际上确实

结果是一个瓶颈,使它们成为非全局的,可以在CPU寄存器中而不是在主存储器中生成
。 (这通常意味着:

让它们成为函数参数并希望最好。)


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


这篇关于更好的解决方案? De-Const'ing变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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