2014-12-04 5 views
5

Ich habe mich nur gefragt, warum alte Compiler wie Turbo C++ 3.0 (Blue Screen IDE) & Borland Turbo C++ 4.5 etc. keinen Fehler im folgenden Programm melden.Warum int & a=10; gilt in alten C++ - Compiler?

Das obige Programm wird von modernen C++ - Compiler nicht akzeptiert, aber warum dann antiken Compiler dies erlaubt? Sie zeigen einfach eine Warnung im obigen Programm an.

+1

Welche Warnung zeigen sie? und welchen Fehler zeigt ein aktueller Compiler? –

+0

@MarkFisher: Warnung: temporär zur Initialisierung von a. – Destructor

+8

gosh Ich erinnere mich an die Zeit, als Borland 4.5 neu war. Wie alt es mich macht? Entschuldigung für die Offtopic: -} – Ashalynd

Antwort

11

Früher war es C++ gültig, um einen Verweis auf ein temporäres zu binden, so dass Sie z. double auf eine Funktion int&, wie erläutert in The Design & Evolution of C++ §3.7 erwartet:

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 (und in ISO C++) Provisorien kann nur auf const Referenzen gebunden.

Verwandte Themen