Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   use of copy constructor with default arguments (http://www.velocityreviews.com/forums/t702172-use-of-copy-constructor-with-default-arguments.html)

Andrey Vul 10-19-2009 09:27 PM

use of copy constructor with default arguments
 
Given the following:

class Bar {};
class Foo {
Bar *parent;
Foo(Bar *p, const Foo& f = 0) {
parent = p;
if (f != 0) {
/* do something with f */
}
}
};

Why does the compiler die while Foo& =0 is used, but works when Foo*
=0 is used?

Andrey Vul 10-19-2009 09:32 PM

Re: use of copy constructor with default arguments
 
On Oct 19, 5:27*pm, Andrey Vul <andrey....@gmail.com> wrote:

> Why does the compiler die while Foo& =0 is used, but works when Foo*
> =0 is used?


Update: the same error condition is given by the compiler as when a
recursive template is unbound (i.e. the famous template factorial
without the factorial<0>)


Alf P. Steinbach 10-19-2009 10:51 PM

Re: use of copy constructor with default arguments
 
* Andrey Vul:
> Given the following:
>
> class Bar {};
> class Foo {
> Bar *parent;
> Foo(Bar *p, const Foo& f = 0) {
> parent = p;
> if (f != 0) {
> /* do something with f */
> }
> }
> };
>
> Why does the compiler die while Foo& =0 is used, but works when Foo*
> =0 is used?


What you wrote is equivalent to

Foo( Bar* p, Foo const& f = Foo( 0 ) )

which if it was valid would be equivalent to

Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )

which if it was valid would be equivalent to

Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )

and so on.


C:\test> gnuc x.cpp
x.cpp: In constructor `Foo::Foo(Bar*, const Foo&)':
x.cpp:6: error: no match for 'operator!=' in 'f != 0'

g++ compiler has a bug, it should have reported error on the argument declaration.


C:\test> msvc x.cpp
x.cpp
x.cpp(4) : fatal error C1202: recursive type or function dependency context too
complex

C:\test> _

Visual C++ compiler also has a bug.

"Doctor, it hurts when I do *this*!" - "Then simply stop doing that."


Cheers & hth.,

- Alf

Andrey Vul 10-20-2009 03:14 AM

Re: use of copy constructor with default arguments
 
On Oct 19, 6:51*pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * Andrey Vul:
>
> > Given the following:

>
> > class Bar {};
> > class Foo {
> > Bar *parent;
> > Foo(Bar *p, const Foo& f = 0) {
> > parent = p;
> > *if (f != 0) {
> > /* do something with f */
> > }
> > }
> > };

>
> > Why does the compiler die while Foo& =0 is used, but works when Foo*
> > =0 is used?

>
> What you wrote is equivalent to
>
> * *Foo( Bar* p, Foo const& f = Foo( 0 ) )
>
> which if it was valid would be equivalent to
>
> * *Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )
>
> which if it was valid would be equivalent to
>
> * *Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )
>
> and so on.
>


So null [pointer] / pure virtual semantic defaults don't work with
references?
Or do they only work when the first argument to the constructor is not
a pointer?
Or does the C++ standard specify nasal demons?

Alf P. Steinbach 10-20-2009 04:00 AM

Re: use of copy constructor with default arguments
 
* Andrey Vul:
> On Oct 19, 6:51 pm, "Alf P. Steinbach" <al...@start.no> wrote:
>> * Andrey Vul:
>>
>>> Given the following:
>>> class Bar {};
>>> class Foo {
>>> Bar *parent;
>>> Foo(Bar *p, const Foo& f = 0) {
>>> parent = p;
>>> if (f != 0) {
>>> /* do something with f */
>>> }
>>> }
>>> };
>>> Why does the compiler die while Foo& =0 is used, but works when Foo*
>>> =0 is used?

>> What you wrote is equivalent to
>>
>> Foo( Bar* p, Foo const& f = Foo( 0 ) )
>>
>> which if it was valid would be equivalent to
>>
>> Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0 ) ) )
>>
>> which if it was valid would be equivalent to
>>
>> Foo( Bar* p, Foo const& f = Foo( 0, Foo( 0, Foo( 0 ) ) ) )
>>
>> and so on.
>>

>
> So null [pointer] / pure virtual semantic defaults don't work with
> references?


Formally there is no nullvalue for a reference.

That's much of the point of references.

Re "pure virtual semantics defaults" I think you're referring to the notation "=
0". There's no such thing for references. It can be meaningful and accepted, but
for a T& reference it then means to construct a T object with 0 as constructor
argument, T(0), since a reference must refer to an object.


> Or do they only work when the first argument to the constructor is not
> a pointer?
> Or does the C++ standard specify nasal demons?


If you happen to dereference a nullpointer you get what in practice would be a
null-reference, except that you've then already crossed the border to Undefined
Behavior land (nasal demons), yes. :-)


Cheers & hth.,

- Alf


All times are GMT. The time now is 01:27 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.