什么是与'const'的交易? [英] Whats the deal with 'const'?

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

问题描述

制作类似于

" const char * x"等形式的参数的函数是什么意思?看来这对函数实际工作和完成它的工作没有影响,也就是说,如果函数没有写给x的b $ b,那么它似乎没有比如编译器可以不关心我是否指定const部分。恰恰相反,如果一个人自由地使用const

然后又回去改变函数,那么当你试图编译并且编译器抱怨时,头痛将不可避免地发生。 />
因为现在突然你正在写x,所以你必须回去

删除const关键字,这可能意味着煞费苦心地删除它

如果有问题的函数是深层嵌套的话,可以从几十行开始

在一系列函数中调用彼此并且都有'const''

关键字。


在这种情况下是const(即将const参数传递给函数)....


1.类似Java的保姆。关键字的前提是所有的b $ b程序员都是白痴并且必须始终保持双手?

2.用于警告编译器某种额外的优化是

可能,如果有问题的数据被操纵,那么这是不可能的?如果是这样的话,这个

究竟会是什么样的优化?

3.关于评论有用,没有其他目的但只是一点点

黄色粘滞便笺说这个功能应该非破坏性地行动

这个特殊的东西?


我总是假设它是1的某种组合3,所以我倾向于

永远不会使用这个东西...


谢谢,这是我一直很好奇的事情= )


Snis Pilbor

解决方案

Snis Pilbor发布:


制作函数的重点是什么?

" const char * x"?



(仅仅是为了迂腐:)


这是一个非const指针变量,指向一个const char 。


看来这对函数

实际工作和完成其工作没有影响,即如果函数不写x / b $ b,然后看起来似乎编译器不关心我是否指定const部分。



调用函数关注:

void Func(char *);


int main (无效)

{

char const c = 0;


Func(& c); / *编译器错误* /

}


恰恰相反,如果使用const

自由然后回去改变功能,头痛

将不可避免地发生,因为一个人试图编译并且编译器抱怨

因为现在突然你写了x,所以你必须回去并且

删除const关键字,如果有问题的函数是深层嵌套的话,这可能意味着从几十行中精心删除


在一系列功能中,彼此调用并且都有'const''

关键字。



修改函数是否改变其参数会改变

函数的性质!


我认为你是const的新手吗? " const的"非常棒。


在任何地方使用它(几乎),你将获得非常强大的

代码。


1.类似Java的保姆关键字是基于所有

程序员都是白痴并且必须始终保持双手的前提?



我会考虑const有两个主要目的:


(1)帮助优化。

(2)如果constness,则使代码无法编译违反。

< OFF-TOPIC>


在C ++中使用函数重载,你可以编写两个完全不同的

函数 - 一个改变它的论点,一个不改变:


void Func(int * p);


void Func (int const * p);


< / OFF-TOPIC>


2.用于提醒编译器某种额外的优化是可能的,如果有问题的数据被操纵,那么这是不可能的?b $ b操纵?如果是这样,那么

究竟会是什么样的优化?



考虑:


int Func(int const x)

{

返回x + 5;

}


也许编译器会重用参数变量......?


3.关于和评论一样有用,没有其他目的但是作为一点点

黄色便条说这个函数应该非破坏性地行动在

这个特殊的东西?



作为这样的指标是可靠的。

别忘了:


修改const对象是未定义的行为。

-


Frederick Gotham


它有助于调试。将const放在原型中允许你1)

发送一个const函数的变量或

函数的非变量变量。如果没有const关键字,您只能向函数发送一个nonconst变量

。 2)当使用引用和指针时,它会阻止

函数修改它,这可以让你隔离bug。


虽然我相信引用与cpp,这是你可能觉得有用的东西。以此定义为例:


void object :: func(const int * const variable)const {; } $ / $

第一个const阻止指针移动(指针

算术),第二个const防止指针指向的数据

来自被操纵,第三个const阻止该函数从

修改属于类object的成员变量。第三个

是一个cpp功能,但其他两个仍然适用于C.虽然我

相信关键字const是一个cpp关键词,而不是C关键词。

我也有一些想法,但我不确定。


我怀疑那里是任何优化,我不知道编译器

在const关键字中产生了什么,所以我不知道。


Snis Pilbor < sn ******** @ yahoo.com写了留言

新闻:11 ********************** @ 35g2000cwc.googlegro ups.com ...


什么是制作函数的重点,这些函数接受像

" const这样的形式的参数char * x?看来这对函数实际工作和完成它的工作没有影响,也就是说,如果函数没有写给x的b $ b,那么它似乎没有比如编译器可以不关心我是否指定const部分。恰恰相反,如果一个人自由地使用const

然后又回去改变函数,那么当你试图编译并且编译器抱怨时,头痛将不可避免地发生。 />
因为现在突然你正在写x,所以你必须回去

删除const关键字,这可能意味着煞费苦心地删除它

如果有问题的函数是深层嵌套的话,可以从几十行开始

在一系列函数中调用彼此并且都有'const''

关键字。


在这种情况下是const(即将const参数传递给函数)....


1.类似Java的保姆。关键字的前提是所有的b $ b程序员都是白痴并且必须始终保持双手?

2.用于警告编译器某种额外的优化是

可能,如果有问题的数据被操纵,那么这是不可能的?如果是这样的话,这个

究竟会是什么样的优化?

3.关于评论有用,没有其他目的但只是一点点

黄色粘滞便笺说这个功能应该非破坏性地行动

这个特殊的东西?


我总是假设它是1的某种组合3,所以我倾向于

永远不会使用这个东西...


谢谢,这是我一直很好奇的事情= )


Snis Pilbor



aragonsr发布:

< blockquote class =post_quotes>
void object :: func(const int * const variable)const {; } $ / $

第一个const阻止指针移动(指针

算术),



第一个const阻止改变指向的数据。


第二个const阻止指针指向的数据

指向被操纵,



第二个const阻止改变指针变量本身。


我怀疑是否有任何优化,我不知道

编译器在const关键字中产生了什么,所以我不知道。



它很可能会改变:


unsigned const len = 5;

unsigned i = len;

简单地说:

unsigned i = 5;

这将对结果机器代码,as,

而不是访问len然后加载一个带有

值的寄存器,它会使用CPU指令直接传递5个。

-


Frederick Gotham


Whats the point of making functions which take arguments of a form like
"const char *x"? It appears that this has no effect on the function
actually working and doing its job, ie, if the function doesn''t write
to x, then it doesnt seem like the compiler could care less whether I
specify the const part. Quite the opposite, if one uses const
liberally and then later goes back and changes the functions, headaches
will inevitably occur as one tries to compile and the compiler gripes
because now suddenly you ARE writing to x, so you have to go back and
remove the const keyword, and this might mean painstakingly removing it
from dozens of lines if the function in question is deeply nested
amidst a family of functions that call eachother and all have ''const''
keywords.

Is const, in this context (ie passing const arguments to functions)....

1. A Java-like "babysitter" keyword based on the premise that all
programmers are idiots and must have their hands held at all times?
2. Used to alert the compiler that some kind of extra optimization is
possible, which wouldn''t be possible if the data in question were
manipulated? And if so, exactly what sort of optimization would this
be?
3. About as useful as a comment, with no other purpose but as a little
yellow sticky note saying "this function should act nondestructively on
this particular thing"?

I always assumed it was some combination of 1 and 3 and so I tend to
never, ever use the thing...

Thanks, this is something I''ve always been curious about =)

Snis Pilbor

解决方案

Snis Pilbor posted:

Whats the point of making functions which take arguments of a form like
"const char *x"?


(Just for sake of pedantry:)

This is a non-const pointer variable which points to a const char.

It appears that this has no effect on the function
actually working and doing its job, ie, if the function doesn''t write
to x, then it doesnt seem like the compiler could care less whether I
specify the const part.


The calling function cares:
void Func(char*);

int main(void)
{
char const c = 0;

Func(&c); /* Compiler ERROR */
}

Quite the opposite, if one uses const
liberally and then later goes back and changes the functions, headaches
will inevitably occur as one tries to compile and the compiler gripes
because now suddenly you ARE writing to x, so you have to go back and
remove the const keyword, and this might mean painstakingly removing it
from dozens of lines if the function in question is deeply nested
amidst a family of functions that call eachother and all have ''const''
keywords.


Revising whether a function alters its argument changes the very nature of
the function!

I take it you''re new to "const"? "const" is brilliant.

Use it (almost) everywhere you can and you''ll be left with very robust
code.

1. A Java-like "babysitter" keyword based on the premise that all
programmers are idiots and must have their hands held at all times?


I would consider "const" to have two main purposes:

(1) To aid in optimisation.
(2) To make code fail to compile if "constness" is violated.
<OFF-TOPIC>

Using function overloading in C++, you can write two totally different
functions -- one which alters its argument, and one which doesn''t:

void Func(int *p);

void Func(int const *p);

</OFF-TOPIC>

2. Used to alert the compiler that some kind of extra optimization is
possible, which wouldn''t be possible if the data in question were
manipulated? And if so, exactly what sort of optimization would this
be?


Consider:

int Func(int const x)
{
return x + 5;
}

Maybe the compiler will re-use the argument variable... ?

3. About as useful as a comment, with no other purpose but as a little
yellow sticky note saying "this function should act nondestructively on
this particular thing"?


It is reliable as such an indicator.
Don''t forget:

It''s undefined behaviour to alter a const object.
--

Frederick Gotham


It helps with debugging. Putting const in the prototype allows you to 1)
send a "const" variable to the function or a nonconst variable to the
function. Without the const keyword, you could only send a nonconst variable
to the function. 2) when using references and pointers, it prevents the
function from modifying it which allows you to isolate bugs.

Although I believe references have to do with cpp, here is something that
you might find useful. Take this definition for example:

void object::func( const int* const variable ) const { ; }

the first const prevents the pointer from be moved around ( pointer
arithmetic ), the second const prevents the data to which the pointer points
to from being manipulated, and the third const prevents the function from
modifying member variables that belong to the class "object". The third one
is a cpp feature, but the other two should still hold true for C. Although I
believe the keyword "const" is a cpp keyword anyways and not a C keyword.
There is some ambituity there too I believe but I''m not sure.

I doubt there is any optimization, I don''t know exactly what the compiler
produces when it comes to the const keyword so I don''t know.

"Snis Pilbor" <sn********@yahoo.comwrote in message
news:11**********************@35g2000cwc.googlegro ups.com...

Whats the point of making functions which take arguments of a form like
"const char *x"? It appears that this has no effect on the function
actually working and doing its job, ie, if the function doesn''t write
to x, then it doesnt seem like the compiler could care less whether I
specify the const part. Quite the opposite, if one uses const
liberally and then later goes back and changes the functions, headaches
will inevitably occur as one tries to compile and the compiler gripes
because now suddenly you ARE writing to x, so you have to go back and
remove the const keyword, and this might mean painstakingly removing it
from dozens of lines if the function in question is deeply nested
amidst a family of functions that call eachother and all have ''const''
keywords.

Is const, in this context (ie passing const arguments to functions)....

1. A Java-like "babysitter" keyword based on the premise that all
programmers are idiots and must have their hands held at all times?
2. Used to alert the compiler that some kind of extra optimization is
possible, which wouldn''t be possible if the data in question were
manipulated? And if so, exactly what sort of optimization would this
be?
3. About as useful as a comment, with no other purpose but as a little
yellow sticky note saying "this function should act nondestructively on
this particular thing"?

I always assumed it was some combination of 1 and 3 and so I tend to
never, ever use the thing...

Thanks, this is something I''ve always been curious about =)

Snis Pilbor



aragonsr posted:

void object::func( const int* const variable ) const { ; }

the first const prevents the pointer from be moved around ( pointer
arithmetic ),


The first const prevents alteration of the data which is pointed to.

the second const prevents the data to which the pointer
points to from being manipulated,


The second const prevents alteration of the pointer variable itself.

I doubt there is any optimization, I don''t know exactly what the
compiler produces when it comes to the const keyword so I don''t know.


It''s very likely to change:

unsigned const len = 5;

unsigned i = len;
into simply:
unsigned i = 5;
This would have an optimising effect on the resultant machine code, as,
rather than accessing "len" and subsequently loading a register with its
value, it would use a CPU instruction whereby it can pass 5 directly.
--

Frederick Gotham


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

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