const正确性 [英] const correctness

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

问题描述

大家好,

我正在浏览产品的源代码。他们从不使用带有默认值的const

函数参数。

例如


从未注意到以下类型的声明


foo(const int param1,const bool = false)const;


而不是

这个

foo(const int param1,bool = false)const;被广泛使用。


我有使用const关键字的默认参数的任何缺点吗?

解决方案

* ma***********@gmail.com


我正在浏览产品的源代码。他们从不使用具有默认值的const
函数参数。

从未注意到以下类型的声明

foo(const int param1,const bool = false)const;

这个
foo(const int param1,bool = false)const;广泛使用。

我有使用const关键字的默认参数的任何缺点吗?




不,两个声明是等价的(并且通过我的意思是,如果添加了返回类型,他们会

指定完全相同的函数签名。


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


ma *********** @ gmail.com 写道:

大家好,

我正在浏览产品的源代码。他们从不使用具有默认值的const
函数参数。

从未注意到以下类型的声明

foo(const int param1,const bool = false)const;

这个
foo(const int param1,bool = false)const;被广泛使用。

我有使用const关键字的默认参数的任何缺点吗?




我不认为有'任何传递值传递参数const的任何一点,

默认值。 const仅影响函数如何使用

参数,并且对调用者没有任何影响。为什么公众

接口声明函数不会修改自己的

参数的私有副本?国际海事组织这样的公共界面混乱了垃圾,这实际上没有来电者的业务。


DW




David White写道:

ma *********** @ gmail.com 写道:

大家好,

我正在经历一个产品的源代码。他们从不使用具有默认值的const
函数参数。

从未注意到以下类型的声明

foo(const int param1,const bool = false)const;

这个
foo(const int param1,bool = false)const;广泛使用。

我有使用const关键字的默认参数的任何缺点吗?



我认为制作中没有任何意义任何pass-by-value参数const,
默认与否。 const仅影响函数如何使用
参数,并且对调用者没有任何影响。为什么公共
接口声明函数不会修改自己的
参数的私有副本? IMO这样的公共界面混乱了垃圾,这实际上并不是调用者的业务。




当你的const对象的行为与no-const不同:


struct test {

void operator()(){}

void运算符()()const {/ *这里可怕的东西* /}

};


Hi Guys,
I was going through a product''s source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?

解决方案

* ma***********@gmail.com:


I was going through a product''s source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?



No, the two declarations are equivalent (and by that I mean, they would
specify exactly the same function signature, if a return type was added).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


ma***********@gmail.com wrote:

Hi Guys,
I was going through a product''s source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?



I don''t think there''s any point in making any pass-by-value parameter const,
default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an
argument? IMO such consts clutter up the public interface with junk that is
really none of the caller''s business.

DW



David White wrote:

ma***********@gmail.com wrote:

Hi Guys,
I was going through a product''s source code. They never use const
function arguments with default value.
for instance

Never noticed following type of declaration

foo ( const int param1, const bool = false) const;

instead
of this
foo ( const int param1, bool = false) const; is used widely.

I there any drawback of using default argument with const keyword?



I don''t think there''s any point in making any pass-by-value parameter const,
default or not. The const only affects how the function can use the
parameter and makes no difference to the caller. Why should the public
interface declare that a function will not modify its own private copy of an
argument? IMO such consts clutter up the public interface with junk that is
really none of the caller''s business.



it''s makes difference when your const object behave not like no-const:

struct test {
void operator () () { }
void operator () () const { /* terrible things here */ }
};


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

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