1

I have some code where IInterface is an abstract class.

I was writing this

IInterface &q  = InterfaceImpl();

and compiled it in Visual Studio 2008 and it worked fine. Then I ported it to a gcc project and suddenly I got this error:

error: invalid initialization of non-const reference of type 'IInterface&' from a temporary of type 'InterfaceImpl'

When trying to find out what is wrong I found this thread error: invalid initialization of non-const reference of type ‘int&’ from an rvalue of type ‘int’ and at least understand now what is wrong. So changing it to this:

InterfaceImpl i = InterfaceImpl();
IInterface &q = i;

made it compile:

However, The lifetime of these two objects are the same, so I don't really understand why gcc can not create a temporary object as apparently MS did. I assume that this is defined by the standard? When I look at the link above, I can understand why it makes no sense with something like a base type, but why does it need to get an error in case of an object?

I also tried it like this, but then I get the error that IInterface is an abstract class and can not be instantiated.

IInterface i = InterfaceImpl();

i should be initialized, not instantiated. Does this mean I would need a copy constructor or assignment operator in InterfaceImpl to make this work or why do I get this error?

Community
  • 1
  • 1
Devolus
  • 21,661
  • 13
  • 66
  • 113
  • 1
    The standard states that you can only bind a temporary object to a const reference. MSVC's behaviour is non-standard. – syam May 18 '13 at 16:03
  • Not directly related to your question, but how about just using `InterfaceImpl q;`? – Vaughn Cato May 18 '13 at 16:14
  • I don't want to use it directly, because this is a simplified version of my project, to develop this part of it, without the overhead of recompiling everything until I know how this should work.The code above would't exist in the real project like this, I just happened to find this issue because of it. – Devolus May 18 '13 at 16:16

1 Answers1

2

InterfaceImpl() returns a temporary object (rvalue) that you cannot keep in a non-const reference.

However when you instantiate IInterface i i becomes an lvalue, So you can keep it in non-const reference.

However binding non-const reference to rvalue is allowed in VS . It has been discussed in this thread

Community
  • 1
  • 1
Neel Basu
  • 12,638
  • 12
  • 82
  • 146
  • Doesn't really make me happy to create a second (useless) variable just to fullfill this requirement. – Devolus May 18 '13 at 16:15
  • @Devolus: In your fake example, you don't need to create a second variable, you can just use the `InterfaceImpl` object directly. If you present a real example, perhaps we can give you an option for that which you don't find so distasteful. – Benjamin Lindley May 18 '13 at 17:03
  • Thanks, but in the real project this variable get assigned by a function anyway, and then I don't have this issue, so It's only an artifact of the example. – Devolus May 18 '13 at 17:05