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!
== Syntax and terminology == === 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> === Rvalue reference === The declaration of the form: <Type>&& <Name> where <code><Type></code> is a type and <code><Name></code> is an identifier is said to define an identifier whose type is '''[[rvalue reference]] to <code><Type></code>'''. Since the name of an [[Value (computer science)#lrvalue|rvalue]] reference is itself an lvalue, <code>std::move</code> must be used to pass an rvalue reference to a function overload accepting an rvalue reference parameter. Rvalue references to cv-unqualified type template parameters of that same function template or <code>auto&&</code> except when deduced from a brace-enclosed initializer list are called '''forwarding references''' (referred to as "universal references" in some older sources<ref>{{cite web|url=http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4164.pdf|title=Forwarding References|last1=Sutter|first1=Herb|last2=Stroustrup|first2=Bjarne|last3=Dos Reis|first3=Gabriel}}</ref>) and can act as lvalue or rvalue references depending on what is passed to them.<ref>[[#cpp-std|ISO/IEC 14822]], clause 13.10.2.1, paragraph 3.</ref> When found in function parameters, they are sometimes used with <code>std::forward</code> to forward the function argument to another function while preserving the value category (lvalue or rvalue) it had when passed to the calling function.<ref>{{cite web|url=http://thbecker.net/articles/rvalue_references/section_01.html|title=C++ Rvalue References Explained|last=Becker|first=Thomas|accessdate=2022-11-25}}</ref> Types which are of kind "reference to <code><Type></code>" are sometimes called '''reference types'''. Identifiers which are of reference type are called '''reference variables'''. To call them ''variable'', however, is in fact a misnomer, as we will see.
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)