我应该总是用'constexpr int'在C ++ 11中尽可能替换'const int'? [英] Should I always replace 'const int' with 'constexpr int' in C++11 whenever possible?

查看:409
本文介绍了我应该总是用'constexpr int'在C ++ 11中尽可能替换'const int'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您会替换

  const int one = 1; 
const int two = 2;

  constexpr int one = 1; 
constexpr int two = 2;

我的理解是否正确,两个块在语义上是否完全相同,



另一方面,由于 constexpr 意味着 const ,你可以认为更一致的是总是喜欢更具限制性的形式,即使在微不足道的情况下,它不会产生影响。



(我理解情况当右侧的表达式被允许更复杂时,完全改变,所以为了澄清,问题只集中在最简单的情况下,表达式是一个固定的整数。)

解决方案

我认为你的语句说 const constexpr 在语义上相同应该被修改:它们都声明其值不能改变的对象,但是 constexpr 也需要initializer表达式在compile-现在如果右侧的表达式不能在编译时计算,可以使用 constexpr



<是出了问题。另一方面,只要初始化器是文字,你可以使用 constexpr ,但是要考虑你的变量的语义是什么:



在SW维护/演化光学器件中,它的值是 可能的话,你将改变在整个时间内初始化变量的方式:今天初始化器是一个字面量,明天它可能是一个更复杂的表达式。



无论您如何为其分配值现在,您认为 i 将需要由字面量以外的任何东西进行初始化,并且初始化表达式在编译时可能无法计算?如果是这样,那么只要使你的变量 const ,即使你正在用字面量初始化它;否则,使它 constexpr



换句话说,选择最能表达变量语义的限定符。


Would you replace

const int one = 1;
const int two = 2;

with this?

constexpr int one = 1;
constexpr int two = 2;

Is my understanding correct that both blocks are semantically identical and that it is currently merely a matter of taste?

On the other hand, as constexpr implies const, you could argue that it is more consistent to always prefer the more restrictive form, even in trivial situations where it does not make a difference?

(I understand that the situation completely changes when the expression on the right side are allowed to be more complicated. So for clarification, the question only focuses on the most simple case where the expression is a fixed integer number.)

解决方案

I think your statement saying that const and constexpr "are semantically identical" should be revised: they both declare objects whose value cannot change, but constexpr also requires the initializer expression to be computable at compile-time.

Now if the expression on the right-hand side cannot be computed at compile-time, using constexpr is out of the question. On the other hand, as long as the initializer is a literal, you could use constexpr, but keep into account what the semantics of your variable is: does your constant variable really represent something whose value should be computable at compile-time?

In a SW maintenance/evolution optics, it is possible that you will change the way you initialize your variable throughout time: today the initializer is a literal, tomorrow it might be a more complex expression.

Regardless of the way you are assigning it a value now, do you think that i will ever need to be initialized by anything else than a literal, and that the initializing expression may not be computable at compile-time? If that is the case, then just make your variable const, even though you are currently initializing it with a literal; otherwise, make it constexpr.

In other words, choose the qualifier that best expresses the semantics of your variable.

这篇关于我应该总是用'constexpr int'在C ++ 11中尽可能替换'const int'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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