返回内置类型的常量值 [英] return const value for built-in type

查看:92
本文介绍了返回内置类型的常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个很好的习惯,为内置类型返回一个const值?

Is it a good custom to return a const value for built-in type?

原因是:

const int F()
{
}

int y;
F(x) = y;

如果返回值为const,上述代码将无法编译。但是
如果不是, F(x)= y; 是一个非常隐蔽的错误。

The above code will not compile if the return value is const. However if it's not, F(x) = y; is a very hidden mistake.

因为它没有意义给一个函数赋值,我认为
是一个很好的习惯总是返回一个const值为内置类型?
如果我总是为内置类型返回一个const值,是否有什么问题?

Since it has no meaning to assign a value to a function, I'm thinking is it a good custom to always return a const value for built-in type? If I always return a const value for built-in type, is there any problem?

推荐答案

const 表示在对象的生命周期内,对象的值不会改变。

const means that during the lifetime of an object, the value of the object does not change.

,const对非类或非类型prvalues的prvalues没有意义。这样的表达式永远不会引用对象(至少如果表达式源自用户代码,语言本身可能在引用绑定期间创建prvalues,而magma引用临时对象),但是这些应该是xvalue。因此,由于没有对象,没有生命。因此,没有什么要举行const。

The Standard itself notes that hence, const does not make sense on prvalues of nonclass or nonarray prvalues. Such expressions never refer to objects (at least if the expression originates from user code. The language itself may create prvalues during reference binding which magically refer to temporary objects. IMHO, though, these should be xvalues instead). Hence since there is no object, there is no lifetime. And hence, there is nothing to be held "const".

这篇关于返回内置类型的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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