“There are just two kinds of languages: the ones everybody complains about and the ones nobody uses.” — Bjarne
This doesn’t compile:
1 2 3 4 5 |
int main(){ int x = 10; double &y = x; // error: invalid initialization of reference of // type 'double&' from expression of type 'int' } |
This does:
1 2 3 4 |
int main(){ int x = 10; const double &y = x; } |
In the second case, y
is initialized by a temporary double
initialized from x
. Not sure what the rationale is but read up TCPPPL 5.5.
Here is where the inconsistency becomes very difficult to ignore:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include using std::cout; using std::endl; int main(){ int x = 10; const int &iy = x; const double &dy = x; x = 20; cout << x << endl; cout << iy << endl; cout << dy << endl; return 0; } |
If anyone knows the reasoning behind this, let me know.