2

Can anyone tell me why visual studio c++ compiler does not allow me to do that:

string& s = "abc";

but I can do that:

string& s = string("abc");

When I tried to compile the second example on linux, the g++ compiler actually threw an error, which is what I would expect. Quick google search told me that in the second case, the lifespan of temporary variable is extended to the lifespan of the reference, but then again, what is wrong with the first example and why g++ complains about it?

zedsdead
  • 51
  • 6
  • 3
    `int i&` doesn't make sense. Do you have `int &i`? – NathanOliver Oct 14 '15 at 14:18
  • You can do this only with const references. That's to do with being able to take a const reference to a temporary - if this didn't exist you'd have dangling references very easily. For non-const references it's invalid (as that argument doesn't apply). Of course, your compiler is free to be buggy. – dascandy Oct 14 '15 at 14:19
  • @NathanOliver You are right, I am very sorry, I was originally testing it on strings and changed it to ints, making a mistake in both my code and the post :/ I edited the question. It will probably make more sense now. – zedsdead Oct 14 '15 at 16:33

1 Answers1

4

There is a simple reason for this. MSVC is buggy. Well-know bug (which they prefer to call a feature) allows int i& = int(5); to compile. It should not.

The correct code:

int main() {
  const int& i = 5;
  const int& j = int(5);
}

As you see, when you are correctly using const reference, both versions work perfectly fine. As to while MSVC allows one incorrect version, and does not allow another - this is simply a bug, and asking 'why the bug behaves in a certain way' is rather moot.

However, if you must reason about bugs :), allowing int& i = 5 would later allow you to write something like i = 10; effectively changing what 5 means within the whole program.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
  • `int i& = int(5);` does not compile with MSVS – NathanOliver Oct 14 '15 at 14:36
  • I don't think the VC++ team looks at it as "bug".. it's a VC++ extension. with move semantics - it's redundand, but pre C++11 this is quite handy optimization – David Haim Oct 14 '15 at 14:39
  • @NathanOliver, hm... Interesting. Why does OP say it compiles? :) – SergeyA Oct 14 '15 at 14:41
  • @DavidHaim, I know what they call it. It is still a bug. – SergeyA Oct 14 '15 at 14:41
  • @SergeyA I believe they have a typo but they have not responded. When I run the code in MSVS it is a compiler error. If they have `int &i` then the question should be closed as a duplicate of http://stackoverflow.com/questions/16380966/non-const-reference-bound-to-temporary-visual-studio-bug – NathanOliver Oct 14 '15 at 14:43
  • @SergeyA it's like saying __async __await on vs2015 is a bug. it's an extension – David Haim Oct 14 '15 at 14:44
  • @DavidHaim, no. __async__await is something which is not covered in the standard. Impementations are free to implement additional keywords in their reserved naming convention - stuff starting with __. On the other hand, reference binding is covered in standard. – SergeyA Oct 14 '15 at 14:58
  • @NathanOliver, may be there are different versions?.. – SergeyA Oct 14 '15 at 14:59
  • @SergeyA The OP has updated there question. You might want to update your answer. – NathanOliver Oct 14 '15 at 16:34
  • @NathanOliver again you are right. What about the case with strings? – zedsdead Oct 14 '15 at 16:36
  • @zedsdead it is the same issue. the duplicate target I posted answers why this works with Microsoft. – NathanOliver Oct 14 '15 at 16:37
  • @NathanOliver I see. So long story short - it's a bug and should not work. That's all I wanted to know. – zedsdead Oct 14 '15 at 16:41