Why int & a = 10; valid in ancient C ++ compilers?

I am just wondering why ancient compilers, such as Turbo C ++ 3.0 (Blue screen IDE) and Borland Turbo C ++ 4.5 etc., do not report any error in the following program.

#include <iostream.h> int main() { int& a=10; cout<<a; return 0; } 

The above program will not be accepted by modern C ++ compilers, but why then do ancient compilers allow this? They simply show one warning in the above program.

+5
source share
1 answer

It used to be valid C ++ to bind a link to a temporary one, so that you could go through, for example. double functions expecting int& , as described in Design and evolution of C ++ ยง3.7:

I made one serious mistake, however, by allowing the link to be initialized not const non-lvalue. [...]
The reason that links must be initialized with non-lvalues โ€‹โ€‹was to allow the difference between a call by value and a call by link to be a part specified by the called function, and is not of interest to the caller. For const links, this is possible; for const links, this is not. For version 2.0, the C ++ definition has been changed to reflect this.

In C ++ 2.0 (and in ISO C ++), temporary links can only be bound to const links.

+11
source

Source: https://habr.com/ru/post/1208333/


All Articles