non-const reference to temporary derived from pure virtual Base

R

reppisch

Hi Ng,

besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.

I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.

Why does this not work with gcc?

To reproduce:
//-----------------------------------
class Base {
public:
Base() {};
virtual void pv() = 0;
virtual ~Base() {};
};

class Derived : public Base {
public:
Derived(int i) {};
virtual void pv() {};
virtual ~Derived() {};
};

class User {
public:
void useBase(Base& b) {};
void useBaseConst(const Base& b) {};
};


int main() {
User u;
Derived d(1);


// Ok with non-temporary !
u.useBase(d);

// not ok!
u.useBase(Derived(1));
//* no matching function for call to `User::useBase(Derived)'
//* candidates are: void User::useBase(Base&)
// seems that the compiler refuses to make a
// non-const reference to a temporary

// but this is ok!
u.useBaseConst(Derived(1));

}

Regards,

Michael
 
M

Marcel Müller

reppisch said:
// not ok!
u.useBase(Derived(1));
//* no matching function for call to `User::useBase(Derived)'
//* candidates are: void User::useBase(Base&)
// seems that the compiler refuses to make a
// non-const reference to a temporary

Exactly that. Temporaries are like rvalues.

I had a similar question some time ago (maybe in the german group).
Somebody explained me that this is intended by the standard, because
there are risks otherwise.

You must create a non-temporary object for this purpuse.

Derived d(1);
u.useBase(d);

Marcel
 
R

reppisch

Hi,

i striped it down further to:

class Derived {
public:
Derived(int i) {};
};

class User {
public:
void useBase(Derived& b) {};
};

int main() {
User u;
u.useBase(Derived(1));
}

I think it's just a "feature" to prevent writing code with surprising
results....
 
E

Erik Wikström

Hi Ng,

besides that this style may be asking for trouble i'm faced with a
unexpected gcc behaviour. Tested with gcc 3.3.3.
On M$ .Net this compiles just fine.

I'm trying to make a non-const reference to a temporary object. The
object is derived from a pure virtual base, so the compiler can't create
a copy of base-type.

Are you sure VS.NET allowed that? If that is the case you really should
check the options you provide to the compiler because making a non-const
reference to a temporary is forbidden by the standard.
 
S

sk_usenet

Erik Wikström said:
Are you sure VS.NET allowed that? If that is the case you really should
check the options you provide to the compiler because making a non-const
reference to a temporary is forbidden by the standard.

Yes, by default VC allows this as an extension to Standrad (I tested a long
time back on probably v7.0). I think it's the /Za option in VC compiler that
would flash the error.
 
A

Andrey Tarasevich

reppisch said:
...
i striped it down further to:
...

You can strip it down to

void foo(int&);
foo(1);

But the bottom line is that explicit binding of non-const references to
temporary objects is illegal in C++.
 
A

Andrey Tarasevich

sk_usenet said:
...
Yes, by default VC allows this as an extension to Standrad (I tested a long
time back on probably v7.0). I think it's the /Za option in VC compiler that
would flash the error.
...

Yes, /Za will fix it.

Without /Za in VS2005 even they implemented a mix of standard behavior
and extended behavior so that the whole thing does seem to satisfy the
formal definition of "compiler extension", while the VC6 was genuinely
and hopelessly broken in that regard.

A plug: see item 2.3 in
http://atarasevich.blogspot.com/2008/02/microsoft-vs2005-c-non-compliance_07.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top