• <tfoot id='UJ2YF'></tfoot><legend id='UJ2YF'><style id='UJ2YF'><dir id='UJ2YF'><q id='UJ2YF'></q></dir></style></legend>

    <small id='UJ2YF'></small><noframes id='UJ2YF'>

        <bdo id='UJ2YF'></bdo><ul id='UJ2YF'></ul>
        <i id='UJ2YF'><tr id='UJ2YF'><dt id='UJ2YF'><q id='UJ2YF'><span id='UJ2YF'><b id='UJ2YF'><form id='UJ2YF'><ins id='UJ2YF'></ins><ul id='UJ2YF'></ul><sub id='UJ2YF'></sub></form><legend id='UJ2YF'></legend><bdo id='UJ2YF'><pre id='UJ2YF'><center id='UJ2YF'></center></pre></bdo></b><th id='UJ2YF'></th></span></q></dt></tr></i><div id='UJ2YF'><tfoot id='UJ2YF'></tfoot><dl id='UJ2YF'><fieldset id='UJ2YF'></fieldset></dl></div>

      1. 变量上的 const 与 constexpr

        时间:2023-05-24
        <tfoot id='VU4zF'></tfoot>

          <small id='VU4zF'></small><noframes id='VU4zF'>

          <i id='VU4zF'><tr id='VU4zF'><dt id='VU4zF'><q id='VU4zF'><span id='VU4zF'><b id='VU4zF'><form id='VU4zF'><ins id='VU4zF'></ins><ul id='VU4zF'></ul><sub id='VU4zF'></sub></form><legend id='VU4zF'></legend><bdo id='VU4zF'><pre id='VU4zF'><center id='VU4zF'></center></pre></bdo></b><th id='VU4zF'></th></span></q></dt></tr></i><div id='VU4zF'><tfoot id='VU4zF'></tfoot><dl id='VU4zF'><fieldset id='VU4zF'></fieldset></dl></div>

              <tbody id='VU4zF'></tbody>
            • <bdo id='VU4zF'></bdo><ul id='VU4zF'></ul>
              <legend id='VU4zF'><style id='VU4zF'><dir id='VU4zF'><q id='VU4zF'></q></dir></style></legend>

                • 本文介绍了变量上的 const 与 constexpr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  以下定义有区别吗?

                  const     double PI = 3.141592653589793;
                  constexpr double PI = 3.141592653589793;
                  

                  如果不是,那么在 C++11 中更喜欢哪种风格?

                  If not, which style is preferred in C++11?

                  推荐答案

                  我相信是有区别的.让我们重命名它们,以便我们可以更轻松地讨论它们:

                  I believe there is a difference. Let's rename them so that we can talk about them more easily:

                  const     double PI1 = 3.141592653589793;
                  constexpr double PI2 = 3.141592653589793;
                  

                  PI1PI2 都是常量,意味着你不能修改它们.但是 PI2 是编译时常量.它在编译时初始化.PI1 可以在编译时或运行时初始化.此外, PI2 可以在需要编译时常量的上下文中使用.例如:

                  Both PI1 and PI2 are constant, meaning you can not modify them. However only PI2 is a compile-time constant. It shall be initialized at compile time. PI1 may be initialized at compile time or run time. Furthermore, only PI2 can be used in a context that requires a compile-time constant. For example:

                  constexpr double PI3 = PI1;  // error
                  

                  但是:

                  constexpr double PI3 = PI2;  // ok
                  

                  和:

                  static_assert(PI1 == 3.141592653589793, "");  // error
                  

                  但是:

                  static_assert(PI2 == 3.141592653589793, "");  // ok
                  

                  至于您应该使用哪个?使用满足您需求的任何一种.你想确保你有一个编译时常量可以在需要编译时常量的上下文中使用吗?您是否希望能够使用在运行时完成的计算来初始化它?等

                  As to which you should use? Use whichever meets your needs. Do you want to ensure that you have a compile time constant that can be used in contexts where a compile-time constant is required? Do you want to be able to initialize it with a computation done at run time? Etc.

                  这篇关于变量上的 const 与 constexpr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++中的变量初始化 下一篇:变量名中的美元符号?

                  相关文章

                  最新文章

                    <tfoot id='Kyap4'></tfoot><legend id='Kyap4'><style id='Kyap4'><dir id='Kyap4'><q id='Kyap4'></q></dir></style></legend>
                    • <bdo id='Kyap4'></bdo><ul id='Kyap4'></ul>

                    <i id='Kyap4'><tr id='Kyap4'><dt id='Kyap4'><q id='Kyap4'><span id='Kyap4'><b id='Kyap4'><form id='Kyap4'><ins id='Kyap4'></ins><ul id='Kyap4'></ul><sub id='Kyap4'></sub></form><legend id='Kyap4'></legend><bdo id='Kyap4'><pre id='Kyap4'><center id='Kyap4'></center></pre></bdo></b><th id='Kyap4'></th></span></q></dt></tr></i><div id='Kyap4'><tfoot id='Kyap4'></tfoot><dl id='Kyap4'><fieldset id='Kyap4'></fieldset></dl></div>

                  1. <small id='Kyap4'></small><noframes id='Kyap4'>