Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Reference (C++)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Lvalue reference === The declaration of the form: <Type>& <Name> where <code><Type></code> is a [[Data type|type]] and <code><Name></code> is an [[identifier]] is said to define an identifier whose type is '''[[Value (computer science)#lrvalue|lvalue]] reference to <code><Type></code>'''.<ref>[[#cpp-std|ISO/IEC 14822]], clause 9.3.3.2, paragraph 1.</ref> Examples: <syntaxhighlight lang="cpp"> int a = 5; int& r_a = a; extern int& r_b; </syntaxhighlight> Here, <code>r_a</code> and <code>r_b</code> are of type "lvalue reference to <code>int</code>" <syntaxhighlight lang="cpp">int& Foo();</syntaxhighlight> <code>Foo</code> is a function that returns an "lvalue reference to <code>int</code>" <syntaxhighlight lang="cpp">void Bar(int& r_p);</syntaxhighlight> <code>Bar</code> is a function with a reference parameter, which is an "lvalue reference to <code>int</code>" <syntaxhighlight lang="cpp">class MyClass { int& m_b; /* ... */ };</syntaxhighlight> <code>MyClass</code> is a <code>class</code> with a member which is lvalue reference to <code>int</code> <syntaxhighlight lang="cpp"> int FuncX() { return 42 ; }; int (&f_func)() = FuncX; int (&&f_func2)() = FuncX; // essentially equivalent to the above </syntaxhighlight> <code>FuncX</code> is a function that returns a (non-reference type) <code>int</code> and <code>f_func</code> is an ''[[aliasing (computing)|alias]]'' for <code>FuncX</code> <syntaxhighlight lang="cpp">const int& ref = 65;</syntaxhighlight> <code>[[const (computer programming)|const]] int& ref</code> is an lvalue reference to <code>const int</code> pointing to a piece of storage having value 65. <syntaxhighlight lang="cpp"> int arr[3]; int (&arr_lvr)[3] = arr; int (&&arr_rvr)[3] = std::move(arr); typedef int arr_t[3]; int (&&arr_prvl)[3] = arr_t{}; // arr_t{} is an array prvalue int *const & ptr_clv = arr; // same as int *const & ptr_clv = &arr[0]; int *&& ptr_rv = arr; // int *&arr_lv = arr; // Error: Initializing an lvalue reference to non-const type with an rvalue </syntaxhighlight> <code>arr_lvr</code> is a reference to an array. When initializing a reference to array, array-to-pointer conversion does not take place, but it does take place when initializing a reference to pointer. Since array-to-pointer conversion returns a prvalue, only lvalue references to <code>const</code> and rvalue references can be initialized with its result. Similarly, when initializing a reference to function, function-to-pointer conversion does not take place (see <code>f_func</code> above), but it does take place when initializing a reference to function pointer: <syntaxhighlight lang="cpp"> int FuncX() { return 42 ; }; int (*const &pf_func)() = FuncX; // same as int (*const &pf_func)() = &FuncX; int (* &&pf_func2)() = FuncX; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)