Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > non-const reference to temporary derived from pure virtual Base

Reply
Thread Tools

non-const reference to temporary derived from pure virtual Base

 
 
reppisch
Guest
Posts: n/a
 
      05-06-2008
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
 
Reply With Quote
 
 
 
 
Marcel Müller
Guest
Posts: n/a
 
      05-06-2008
reppisch schrieb:
> // 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
 
Reply With Quote
 
 
 
 
reppisch
Guest
Posts: n/a
 
      05-06-2008
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....
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      05-06-2008
On 2008-05-06 16:41, reppisch wrote:
> 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.

--
Erik Wikström
 
Reply With Quote
 
sk_usenet
Guest
Posts: n/a
 
      05-06-2008
"Erik Wikström" <(E-Mail Removed)> wrote in message
> On 2008-05-06 16:41, reppisch wrote:
>> 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.


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.

--
http://techytalk.googlepages.com


 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-06-2008
reppisch wrote:
> ...
> 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++.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      05-06-2008
sk_usenet wrote:
> ...
> 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...liance_07.html

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Format of compiler generated derived destructor when base has 'virtual ~base() throw():" qazmlp C++ 1 04-10-2005 03:09 PM
If declared as virtual in base, its derived version also is virtual. Why not for destructors? qazmlp C++ 7 07-27-2004 03:10 PM
Re: pure virtual functions as non-const in derived classes Gianni Mariani C++ 2 08-27-2003 09:57 PM



Advertisments