const和constexpr最终是一样的东西吗? [英] Will const and constexpr eventually be the same thing?

查看:203
本文介绍了const和constexpr最终是一样的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是阅读

const vs constexpr on variables

,并正在观看此 Google Tech Talk关于C ++ 11/14功能,其中说,好的,constexpr在未来可能不是必要的,当涉及到功能,因为编译器将演变以自己计算出来。最后,我现在Java编译器和JVM努力工作,以确定类(或任何变量也许)在构造之后是不可变的 - 没有你明确地说,并且基于这个事实进行各种恶劣的优化。

and am watching this Google Tech Talk about C++11/14 features , in which it is said that, well, constexpr might not be necessary in the future when it comes to functions, since compilers will evolve to figure it out on their own. Finally, I now that Java compilers and JVMs work hard to figure out that classes (or any variable maybe) are immutable after construction - without you explicitly saying so - and doing all sorts of wicked optimization based on this fact.

所以,这里的问题:const和constexpr的命运最终是一样的东西吗?也就是说,即使编译器不能保证做运行时初始化等,它将不会最终这样做只要可能(基本上)?当这种情况发生时,不会有一个关键字是多余的?

So, here's the question: Is the fate of const and constexpr to eventually be the same thing? That is, even though a compiler is not guaranteed to do runtime initialization etc., will it not eventually do so whenever possible (basically)? And when that happens, won't one of the keywords be redundant? (Just like inline is becoming, maybe)?

推荐答案

不,任何一个都不会替换另一个,他们有不同的角色。 Bjarne Stroustrup告诉我们在他的 C ++常见问题中,constexpr不是const的替代并概述了每个特性的不同角色:

No, neither one will replace the other, they have different roles. Bjarne Stroustrup tells us in his C++ FAQ that constexpr is not a replacement for const and outlines the different roles of each feature:


请注意,constexpr不是通用替换
const反之亦然):

Please note that constexpr is not a general purpose replacement for const (or vise versa):


  • const 主要功能是表达一个对象不通过接口修改的想法即使对象可能很好地通过其他接口修改
    )。它只是这样发生,声明
    对象 const
    编译器提供了优秀的优化机会。特别是,如果一个对象被声明为 const 并且它的
    地址没有被采用,编译器通常能够在编译时评估它的
    初始化器(虽然不能保证)

  • constexpr的主要功能是扩展范围,并在其表中保留
    对象,而不是将其发送到生成的
    代码。在编译时可以计算什么,使得这种计算类型安全。对象
    声明 constexpr 在编译时评估其初始化器;
    他们基本上是保存在编译器表中的值,只有
    发射到生成的代码中。

  • const's primary function is to express the idea that an object is not modified through an interface (even though the object may very well be modified through other interfaces). It just so happens that declaring an object const provides excellent optimization opportunities for the compiler. In particular, if an object is declared const and its address isn't taken, a compiler is often able to evaluate its initializer at compile time (though that's not guaranteed) and keep that object in its tables rather than emitting it into the generated code.
  • constexpr's primary function is to extend the range of what can be computed at compile time, making such computation type safe. Objects declared constexpr have their initializer evaluated at compile time; they are basically values kept in the compiler's tables and only emitted into the generated code if needed.

这篇关于const和constexpr最终是一样的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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