Why int& a=10; is valid in ancient C++ compilers?
Asked Answered
R

1

5

I was just wondering why ancient compilers like Turbo c++ 3.0(Blue screen IDE) & Borland Turbo C++ 4.5 etc doesn't report any error in following program.

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

The above program won't be accepted by modern C++ compilers, But why then ancient compilers allows this? They simply shows single warning in above program.

Render answered 4/12, 2014 at 8:57 Comment(16)
what warning do they show? and what error does an up to date compiler show?Anatomize
@MarkFisher: warning: temporary used to initialize a.Render
gosh I remember the time when Borland 4.5 was new. how ancient it makes me? sorry for the offtopic :-}Southbound
@meet apparently ancient compilers allowed to have a reference to a temporary variable. Borland, at least, gave a warning, while e.g. g++ didn't. gcc.gnu.org/bugzilla/show_bug.cgi?id=986Southbound
Who cares what ancient rubbish compilers do?Misguide
@MarkFisher: error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ int& a=10;Render
I used Borland C 2.0 in DOS for short time, then moved to 3.1 under Windows 3.1. It had good IDE and editor. But I have used references hardly ever. I think references are mostly for "pascal people". There are pointers which give all you need.Adsorbate
@Ashalynd: Did you mean ancient compilers create temporary variable in this case?Render
Turbo and Borland C++ are older than the first C++ standard so those non-standard things may not be surprisedJeniferjeniffer
@meet most probably so, otherwise this assignment would not be possible.Southbound
@Ashalynd: exactly.I was thinking sameRender
Visual Studio still allows this.Gaylor
@Ashalynd, that GCC bug report is a completely different case, that's binding a temporary to const reference, which is valid today in C++. This question is only about non-const references.Armour
@SebastianRedl MSVC has many non-standard extensions. From Wakely's answer it's clear that it was allowed at one point. You can't violate the standard that doesn't exist. I can't reproduce though, what version are you using?Whiz
VS 2013, and VS 2015 still has this non-standard extension. However, apparently VS only allows it for class types. (Doesn't work with int, works with struct S {};.) There's a Level 4 warning issues when you do this.Gaylor
Warning are logical errors in your code. Your code should compile with zero warnings.Pudding
A
11

It used to be valid C++ to bind a reference to a temporary, so you could pass e.g. double to a function expecting int&, as explained 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 (and in ISO C++) temporaries can only be bound to const references.

Armour answered 4/12, 2014 at 9:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.