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!
== Meaning and limitations == References are not objects and references can only refer to object or function types. Arrays of references, pointers to references and references to references are not allowed because they require object types. {{cpp|int& i[4]}}, {{cpp|int&*i}} and {{cpp|int& &i}} will cause compilation errors (while {{cpp|int(& i)[4]}} (reference of array) and {{cpp|int*&i}} (reference of pointer) will not assuming they are initialized). References to <code>void</code> are also ill-formed because <code>void</code> is not an object or function type, but references to <code>void *</code> can exist. Declaring references as const or volatile({{cpp|int& volatile i}}) also fails unless a typedef/decltype is used in which case the const/volatile is ignored. However, if template argument deduction takes place and a reference type is deduced (which happens when forwarding references are used and an lvalue is passed to the function) or if <code>[[typedef]]</code>, <code>[[C++11#Template aliases|using]]</code> or <code>[[decltype]]</code> denote a reference type it is possible to take a reference to that type. In that case the rule that is used to determine the type of reference is called reference collapsing and works like this: Assuming a type <code>T</code> and a reference type to <code>T</code> <code>TR</code>, attempting to create an rvalue reference to <code>TR</code> creates a <code>TR</code> while an lvalue reference to <code>TR</code> creates an lvalue reference to <code>T</code>. In other words, lvalue references override rvalue references and rvalue references of rvalue references stay unchanged. <syntaxhighlight lang="cpp"> int i; typedef int& LRI; using RRI = int&&; LRI& r1 = i; // r1 has the type int& const LRI& r2 = i; // r2 has the type int& const LRI&& r3 = i; // r3 has the type int& RRI& r4 = i; // r4 has the type int& RRI&& r5 = 5; // r5 has the type int&& decltype(r2)& r6 = i; // r6 has the type int& decltype(r2)&& r7 = i; // r7 has the type int& </syntaxhighlight> A non-static [[member function]] can be declared with a ref qualifier. This qualifier participates in [[overload resolution]] and applies to the implicit object parameter like <code>const</code> and <code>[[volatile_(computer_programming)#In_C_and_C.2B.2B|volatile]]</code> but unlike those two, it does not change the properties of <code>[[This_(computer_programming)#C++|this]]</code>. What it does is mandate that the function be called on an lvalue or rvalue instance of the class. <syntaxhighlight lang="cpp"> #include <iostream> struct A { A() = default; void Print()const& { std::cout << "lvalue\n"; } void Print()const&& { std::cout << "rvalue\n"; } }; int main() { A a; a.Print(); // prints "lvalue" std::move(a).Print(); // prints "rvalue" A().Print(); // prints "rvalue" A&& b = std::move(a); b.Print(); // prints "lvalue"(!) } </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)