<i id='fjSJg'><tr id='fjSJg'><dt id='fjSJg'><q id='fjSJg'><span id='fjSJg'><b id='fjSJg'><form id='fjSJg'><ins id='fjSJg'></ins><ul id='fjSJg'></ul><sub id='fjSJg'></sub></form><legend id='fjSJg'></legend><bdo id='fjSJg'><pre id='fjSJg'><center id='fjSJg'></center></pre></bdo></b><th id='fjSJg'></th></span></q></dt></tr></i><div id='fjSJg'><tfoot id='fjSJg'></tfoot><dl id='fjSJg'><fieldset id='fjSJg'></fieldset></dl></div>
    <legend id='fjSJg'><style id='fjSJg'><dir id='fjSJg'><q id='fjSJg'></q></dir></style></legend>
    • <bdo id='fjSJg'></bdo><ul id='fjSJg'></ul>
  1. <small id='fjSJg'></small><noframes id='fjSJg'>

    1. <tfoot id='fjSJg'></tfoot>

      如何初始化 constexpr 引用

      how to initialize a constexpr reference(如何初始化 constexpr 引用)

        • <legend id='ZywAG'><style id='ZywAG'><dir id='ZywAG'><q id='ZywAG'></q></dir></style></legend>
              <tbody id='ZywAG'></tbody>
            <i id='ZywAG'><tr id='ZywAG'><dt id='ZywAG'><q id='ZywAG'><span id='ZywAG'><b id='ZywAG'><form id='ZywAG'><ins id='ZywAG'></ins><ul id='ZywAG'></ul><sub id='ZywAG'></sub></form><legend id='ZywAG'></legend><bdo id='ZywAG'><pre id='ZywAG'><center id='ZywAG'></center></pre></bdo></b><th id='ZywAG'></th></span></q></dt></tr></i><div id='ZywAG'><tfoot id='ZywAG'></tfoot><dl id='ZywAG'><fieldset id='ZywAG'></fieldset></dl></div>
            1. <small id='ZywAG'></small><noframes id='ZywAG'>

                <bdo id='ZywAG'></bdo><ul id='ZywAG'></ul>
                <tfoot id='ZywAG'></tfoot>

                本文介绍了如何初始化 constexpr 引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!


                我试图初始化一个 constexpr 引用,但没有成功.我试过了

                #include constexpr int&f(int& x)//可以定义返回 constexpr 引用的函数{返回 x;}int main(){constexpr int x{20};constexpr const int&z = x;//此处出错}



                错误:constexpr 变量 'z' 必须由常量表达式初始化

                删除 const 导致


                错误:将int"类型的引用绑定到const int"类型的值会丢弃限定符

                尽管我感觉 constexpr 自动暗示了变量声明的 const.


                1. constexpr 引用有用吗?(即,比 const 引用更好")
                2. 如果是,我如何有效地定义它们?

                PS:我看到了一些与我的相关的问题,例如 哪些值可以分配给 `constexpr` 引用? ,但我认为他们没有解决我的问题.


                1. constexpr 引用有用吗?(即,比 const 引用更好")

                它们保证在程序启动之前被初始化,而对 const 的引用可以在程序开始运行后的动态初始化期间初始化.


                1. 如果是,我如何有效地定义它们?

                constexpr 引用必须绑定到全局变量,而不是局部变量(或者更正式地说,它必须绑定到具有静态存储持续时间的东西).


                I am trying to initialize a constexpr reference with no success. I tried

                #include <iostream>
                constexpr int& f(int& x) // can define functions returning constexpr references
                    return x;
                int main()
                    constexpr int x{20};
                    constexpr const int& z = x; // error here

                but I'm getting a compile time error

                error: constexpr variable 'z' must be initialized by a constant expression

                Dropping the const results in

                error: binding of reference to type 'int' to a value of type 'const int' drops qualifiers

                even though I had the feeling that constexpr automatically implies const for variable declarations.

                So my questions are:

                1. Are constexpr references ever useful? (i.e., "better" than const references)
                2. If yes, how can I effectively define them?

                PS: I've seen a couple of questions related to mine, such as Which values can be assigned to a `constexpr` reference? , but I don't think they address my questions.


                1. Are constexpr references ever useful? (i.e., "better" than const references)

                They are guaranteed to be initiailized before the program starts, whereas a reference to const can be initialized during dynamic initialization, after the program starts running.

                1. If yes, how can I effectively define them?

                A constexpr reference has to bind to a global, not a local variable (or more formally, it has to bind to something with static storage duration).

                A reference is conceptually equivalent to taking the address of the variable, and the address of a local variable is not a constant (even in main which can only be called once and so its local variables are only initialized once).

                这篇关于如何初始化 constexpr 引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



                C++ stl unordered_map implementation, reference validity(C++ stl unordered_map 实现,参考有效性)
                C++: Is it possible to use a reference as the value in a map?(C++:是否可以使用引用作为映射中的值?)
                Where ampersand quot;amp;quot; can be put when passing argument by reference?(其中符号“amp;通过引用传递参数时可以放置吗?)
                Why can a non-const reference parameter be bound to a temporary object?(为什么可以将非常量引用参数绑定到临时对象?)
                What is a dangling reference?(什么是悬空引用?)
                C++ reference changes when push_back new element to std::vector(当 push_back 新元素到 std::vector 时,C++ 引用发生变化)

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

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

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

                            <tbody id='aZYhC'></tbody>