2014-12-04 11 views
5

Mi stavo chiedendo perché compilatori come Turbo C++ 3.0 (Blue screen IDE) & Borland Turbo C++ 4.5 ecc. Non riportano alcun errore nel seguente programma.Perché int & a=10; è valido in antichi compilatori C++?

Il programma di cui sopra non sarà accettato dai compilatori C++ moderni, ma perché quindi i compilatori antichi lo consentono? Mostrano semplicemente un singolo avviso nel programma sopra.

+1

quale avviso mostrano? e quale errore mostra un compilatore aggiornato? –

+0

@MarkFisher: avviso: temporaneo utilizzato per inizializzare a. – Destructor

+8

Mi ricordo quando Borland 4.5 era nuovo. quanto mi rende antico? mi dispiace per l'offtopic: -} – Ashalynd

risposta

11

È stato utilizzato C++ per associare un riferimento a un riferimento temporaneo, quindi è possibile passare ad es. double a una funzione in attesa int&, come spiegato in The Design & Evolution of C++ §3.7:

I made one serious mistake, though, by allowing a non- const reference to be initialized by a non-lvalue. [...]
The reason to allow references to be initialized by non-lvalues was to allow the distinction between call-by-value and call-by-reference to be a detail specified by the called function and of no interest to the caller. For const references, this is possible, for non- const references it is not. For Release 2.0 the definition of C++ was changed to reflect this.

In C++ 2.0 (e in ISO C++) provvisori possono essere usate solo const riferimenti.

Problemi correlati