什么是“哲学”? const的意思? [英] What is the "philosophical" meaning of const?

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

问题描述

您好,


我知道C ++中const处理的规则,但是我想问一下

的正确是什么。使用它们的方式,例如。什么时候合适成员?

函数const?


当我考虑实现时遇到这个问题

实现一些惰性数据结构或缓存的类的C ++规则

允许使所有成员函数非const,

或使所有成员函数const和所有成员都可变。这些是极端的可能性,但在我考虑的情况下,

的差异更为微妙。想象一下,例如,一个懒惰的数据

结构,就像带有路径压缩的union-find数据结构一样。

这个数据结构维护一些集合的分区,有两个主要的

操作:


1. union:对于分区中两个子集的代表,改变

数据结构,以便分区包含除了两个之外的所有子集,

由他们的联合替换。


2.找到:对于该集合的成员,找到一个代表子集它

在。


从这个描述中,似乎union是非const的,并且找到了

const 。但是如果你看一下实现,find会改变数据

结构。它是const还是不是?


const更像是不触及对象的内部状态或者你

不会注意到状态发生了变化?


编译器对此的看法是什么?他们使用const来进行一些优化吗?是否存在使用强制转换删除const

的危险(这显然违反了const的目的)?


问候

Jiri Palecek

解决方案

=?UTF-8?B?SmnFmcOtIFBhbGXEjWVr?=发布:


您好,


我知道C ++中const处理的规则,但是我想问一下

的正确是什么。使用它们的方式,例如。什么时候适合成员?

函数const?



我提倡的经验法则是:尽可能使事情成为常数。


然而有例外:


(1)按值返回的函数


int Func();


优先于:


int const Func();


(2)投射产生R值


(int)


优先于:


(int const)


我之所以不在这些地方使用const,是因为它会多余,因为无论如何都不能改变R值。

< blockquote class =post_quotes>
编译器对此的看法是什么?他们使用const来进行一些优化吗?



是的,相当多。


使用强制转换是否有任何危险删除const

(这显然违反了const的目的)?



如果更改const对象,则调用未定义的行为。


-


Frederick Gotham


Ji ?? Pale?ek< jp ****** @ web.dewrote:


我知道C ++中const处理的规则,但我想问什么是

正确的使用它们的方式,例如。何时合适成员

函数const?



[snip]


>

const更像是不触及对象的内部状态或者你

不会注意到状态发生了变化?



查看Const正确性"常见问题解答部分。特别是在

中,18.10说,

[a]成员函数的尾随常量意味着抽象

(客户端 - 可见的)对象的状态不会改变。这与承诺的原始位略有不同。对象的'

结构不会改变。


所以,我会说它会更符合逻辑常量(& ;你不会告诉状态改变了b $ b。


编译器对此的看法是什么?他们使用const来进行一些优化吗?使用强制转换删除const

是否有任何危险(这明显违反了const的目的)?



他们也会在常见问题解答中触及这些主题。基本上,更喜欢

可变成员到const_casts,但当然你的

计划的细节可能另有要求。


-

Marcus Kwok

用''net''替换''invalid''回复


Frederick Gotham发布了:


我在这些地方不使用const的原因是因为它会是多余的,因为R值可以无论如何都不要改变。



除非我们正在处理用户定义的类型;但是,我仍然没有b $ b认为我会使用const,例如:


#include< string>

使用std :: string;


void Func(string const&){}


void Func(char const *){}


int main()

{

Func(string(" Hello"));

}


(注意:


string(" Hello")


相当于:


(字符串)" Hello"


NOT:


( string const)你好


-


Frederick Gotham


Hello,

I know the rules for const handling in C++, but I''d like to ask what is
the "right" way to use them, eg. when is it appropriate to make a member
function const?

This came across this question when I was thinking about implementation
of a class implementing some lazy data structure or cache. The C++ rules
allow among other possibilities making all member functions non-const,
or making all member functions const and all members mutable. These are
the extreme possibilities, but in cases I was thinking about, the
differences are more subtle. Imagine, for example, a lazy data
structure, like the union-find data structure with path compression.
This data structure maintains a partition of some set and has two major
operations:

1. union: for two representatives of subsets in the partition, alter the
data structure so the partition contains all subsets except the two,
which are replaced by their union.

2. find: for a member of the set, find a representative of the subset it
is in.

From this description, it may seem that union is non-const and find is
const. But if you look at the implementation, the find alters the data
structure. So is it const or not?

Is const more like "doesn''t touch the inner state of the object" or "you
won''t notice the state changed"?

What is the compilers'' view on this? Do they use const to make some
optimizations? Is there any danger to use cast to remove the const
(which clearly violates the purpose of the const)?

Regards
Jiri Palecek

解决方案

=?UTF-8?B?SmnFmcOtIFBhbGXEjWVr?= posted:

Hello,

I know the rules for const handling in C++, but I''d like to ask what is
the "right" way to use them, eg. when is it appropriate to make a member
function const?


The rule of thumb I advocate is: Make things const whenever you can.

There are exceptions however:

(1) Functions which return by value

int Func();

is prefereable over:

int const Func();

(2) Casts which yield an R-value

(int)

is preferable over:

(int const)

The reason why I don''t use const in these places is because it would be
redundant, because R-values can''t be altered in anyway.

What is the compilers'' view on this? Do they use const to make some
optimizations?


Yes, quite a lot.

Is there any danger to use cast to remove the const
(which clearly violates the purpose of the const)?


If you alter a const object, you invoke undefined behaviour.

--

Frederick Gotham


Ji?? Pale?ek <jp******@web.dewrote:

I know the rules for const handling in C++, but I''d like to ask what is
the "right" way to use them, eg. when is it appropriate to make a member
function const?

[snip]

>
Is const more like "doesn''t touch the inner state of the object" or "you
won''t notice the state changed"?

Have a look at the "Const correctness" section of the FAQ. In
particular, 18.10 says,

The trailing const on [a] member function means that the abstract
(client-visible) state of the object isn''t going to change. This is
slightly different from promising that the "raw bits" of the object''s
struct aren''t going to change.

So, I would say that it would be more for logical constness ("you won''t
notice the state changed").

What is the compilers'' view on this? Do they use const to make some
optimizations? Is there any danger to use cast to remove the const
(which clearly violates the purpose of the const)?

They touch on these topics in the FAQ as well. Basically, prefer
mutable members to const_casts, but of course the specifics of your
program may require otherwise.

--
Marcus Kwok
Replace ''invalid'' with ''net'' to reply


Frederick Gotham posted:

The reason why I don''t use const in these places is because it would be
redundant, because R-values can''t be altered in anyway.


Unless of course we''re dealing with user-defined types; but still, I don''t
think I''d use const, e.g.:

#include <string>
using std::string;

void Func(string const&) {}

void Func(char const*) {}

int main()
{
Func( string("Hello") );
}

(Note that:

string("Hello")

is equivalent to:

(string)"Hello"

NOT:

(string const)"Hello"

--

Frederick Gotham


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

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