Why is it allowed to pass R-values ​​via a constant link, but not a regular link?

as the title says why it is allowed to pass R-values ​​(literals) via a permalink, but is not a normal link

void display(const int& a)
{
cout << a ;
}

will work if called display(5), but without constit it will not work ****** I mean, how the link constshould point to the value of R (anonymous variable) *** ***

+14
source share
5 answers

For the last question:

as a constant reference refers to the value of R (anonymous variable)

. ++ , , (.. ).

+17

- , , - , , .. variable literal. , .


Reference - .

int a = 5;
int &b = a;

( 5 ).

const int &c = 5;

, .


, , const. , , ( ) literal.

+6

. ; , 5 , 5 . , , , , , . , ++ , , - , , .

, !

+3

. 1 2, "abd" "edf".

++ const, :

  • , 1 2.
  • , , , , undefined, .

(1) , x == 1 , " 2" , (2) , void display(int& a) , ?

, const.


, char* , , , . char* "abc", "" char* char undefined. ( , ).

, ?

+1

r , , . , ( ?)

++ . r- (non-const), . , r , .

, r- ( &&). , , , , , , r-value.

+1
source

All Articles