use of copy constructor with default arguments

A

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?
 
A

Andrey Vul

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>)
 
A

Alf P. Steinbach

* 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
 
A

Andrey Vul

* Andrey Vul:




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?
 
A

Alf P. Steinbach

* Andrey Vul:
* Andrey Vul:

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
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top