S
Ich finde die Stelle, wo das steht, eben nicht wieder.
§8.5.3 schrieb:
4.
Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of T2. “cv1 T1” is reference-compatible with “cv2 T2” if T1 is reference-related to T2 and cv1 is the same cv-qualification as, or greater cv-qualification than, cv2.
5.
A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:
If the reference is an lvalue reference and the initializer expression
is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or
has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be implicitly converted to an lvalue of type “cv3 T3,” where “cv1 T1” is reference-compatible with “cv3 T3”106 (this conversion is selected by enumerating the applicable conversion functions (13.3.1.6) and choosing the best one through overload resolution (13.3)),
then the reference is bound to the initializer expression lvalue in the first case and to the lvalue result of the conversion in the second case (or, in either case, to the appropriate base class subobject of the object).
Eine non-const lvalue-Referenz muss also immer zu einem non-const lvalue gebunden werden.
Das waren die gewöhnlichen Semantiken für lvalue-Referenzen.
Erst im nächsten Abschnitt wird dann für const-lvalue -Referenzen erklärt..
Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference.
If the initializer expression
is an xvalue, class prvalue, array prvalue or function lvalue and “cv1 T1” is reference compatible with “cv2 T2”, or
has a class type (i.e., T2 is a class type), where T1 is not reference-related to T2, and can be implicitly converted to an xvalue, class prvalue, or function lvalue of type “cv3 T3”, where “cv1 T1” is reference-compatible with “cv3 T3”
then the reference is bound to the value of the initializer expression in the first case and to the result of the conversion in the second case (or, in either case, to an appropriate base class subobject). [...]
Otherwise, a temporary of type “cv1 T1” is created and initialized from the initializer expression using the rules for a non-reference copy-initialization (8.5). The reference is then bound to the temporary.
Achtung: Die letzten beiden Sätze treffen nicht immer zu.
schreibt, dann trifft das auf die obige Erklärung zu;
std::string const& = std::string{};
Wenn man jedoch
std::string const& = "ABC";
Dann trifft es auf die letzte Erklärung zu.
Zumindest nicht ohne dass man einen eigenen Zuweisungsoperator schreibt.
Wie sollen sie dann zuweisbar sein? Es ist klar festgelegt, dass eine Referenz ihren Referenten nicht ändern kann:
A reference cannot be changed to refer to another object after initialization. Note that initialization of a reference is treated very differently from assignment to it.
Daher ist in Klassen immer ein Zeiger besser aufgehoben.