* Copy constructor with non const rhs arg
@ 2004-05-12 15:06 paul moore
2004-05-12 16:05 ` Eljay Love-Jensen
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: paul moore @ 2004-05-12 15:06 UTC (permalink / raw)
To: gcc-help
This code does not compile on gcc (3.2.2 on rh9 - 3.3 on suse 9)
class test
{
public:
int x;
test(test &rhs)
{
x = rhx.x;
}
};
test foo();
int main(int argc, char* argv[])
{
test dd = foo();
return 0;
}
The test dd line gets hit saying there is no match for test::test(test), the
candidates are test::test(test&)
If I change the copy constructor to 'test(const test&)' it compiles fine.
What am I trying to do? In real life the class contains an auto_ptr - since
the auto_ptr copy constructor updates the rhs you cannot pass it in with
const. (Note the auto_ptr copy constructor itself is declared with a non
const rhs - and code using it compiles fine - this is really puzzling)
Also if I change foo to 'test &foo()' it compiles fine (but of course this
is totally different semantics)
Note also that MSFT vs 2003 compiles this code quite happily.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Copy constructor with non const rhs arg
2004-05-12 15:06 Copy constructor with non const rhs arg paul moore
@ 2004-05-12 16:05 ` Eljay Love-Jensen
2004-05-12 16:15 ` Eljay Love-Jensen
2004-05-12 16:43 ` Tony Wetmore
2004-05-12 16:26 ` Eljay Love-Jensen
2004-05-12 18:08 ` llewelly
2 siblings, 2 replies; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-05-12 16:05 UTC (permalink / raw)
To: paul moore, gcc-help
Hi Paul,
I believe a copy constructor requires a const parameter.
But I may be mistaken. I've been mistaken before.
You can make the auto_ptr member variable mutable.
--Eljay
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Copy constructor with non const rhs arg
2004-05-12 16:05 ` Eljay Love-Jensen
@ 2004-05-12 16:15 ` Eljay Love-Jensen
2004-05-12 16:43 ` Tony Wetmore
1 sibling, 0 replies; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-05-12 16:15 UTC (permalink / raw)
To: paul moore, gcc-help
Hi Paul,
How about pass-by-value instead of pass-by-reference?
After all, an auto_ptr as a parameter should always be
pass-by-value. That's the semantic of pass-by-value for auto_ptr: a
transfer of ownership.
--Eljay
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Copy constructor with non const rhs arg
2004-05-12 15:06 Copy constructor with non const rhs arg paul moore
2004-05-12 16:05 ` Eljay Love-Jensen
@ 2004-05-12 16:26 ` Eljay Love-Jensen
2004-05-12 18:08 ` llewelly
2 siblings, 0 replies; 7+ messages in thread
From: Eljay Love-Jensen @ 2004-05-12 16:26 UTC (permalink / raw)
To: paul moore, gcc-help
hi Paul,
Hmm, I guess pass-by-value a Copy Constructor doth not make.
Solution #1
--------------------------------
#include <memory>
using namespace std;
class Foo
{
public:
Foo(Foo const& in);
private:
auto_ptr<int> m;
};
Foo::Foo(Foo const& in)
: m(const_cast<Foo&>(in).m)
{
}
Foo FooFactory();
int main()
{
Foo f(FooFactory());
Foo f2 = FooFactory();
}
--------------------------------
Solution #2
--------------------------------
#include <memory>
using namespace std;
class Foo
{
public:
Foo(Foo const& in);
private:
mutable auto_ptr<int> m;
};
Foo::Foo(Foo const& in)
: m(in.m)
{
}
Foo FooFactory();
int main()
{
Foo f(FooFactory());
Foo f2 = FooFactory();
}
--------------------------------
HTH.
--Eljay
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: Copy constructor with non const rhs arg
2004-05-12 16:05 ` Eljay Love-Jensen
2004-05-12 16:15 ` Eljay Love-Jensen
@ 2004-05-12 16:43 ` Tony Wetmore
1 sibling, 0 replies; 7+ messages in thread
From: Tony Wetmore @ 2004-05-12 16:43 UTC (permalink / raw)
To: 'Eljay Love-Jensen', 'paul moore', gcc-help
(I sent this earlier to Paul but forgot to CC the list...)
I believe the problem here is that "test dd = foo()" is using a
temporary object (returned by foo). You cannot pass a non-const
reference to that temporary object into test's copy constructor.
Temporary objects are always "const".
You can, of course, pass a const reference to a temporary object.
Similarly, if foo() returns a reference, that reference must point to a
valid (not temporary) object, so passing a reference to THAT object into
test's copy constructor is just fine.
So I think gcc's behavior is correct (though the error text is a bit
unclear). I don't know what Microsoft's compiler is doing in that case,
but it seems unusual and could probably lead to bad behavior.
---
Tony Wetmore
Raytheon Solipsys
mailto:tony.wetmore@solipsys.com
http://www.solipsys.com
-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Eljay Love-Jensen
Sent: Wednesday, May 12, 2004 12:05 PM
To: paul moore; gcc-help@gcc.gnu.org
Subject: Re: Copy constructor with non const rhs arg
Hi Paul,
I believe a copy constructor requires a const parameter.
But I may be mistaken. I've been mistaken before.
You can make the auto_ptr member variable mutable.
--Eljay
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: Copy constructor with non const rhs arg
2004-05-12 15:06 Copy constructor with non const rhs arg paul moore
2004-05-12 16:05 ` Eljay Love-Jensen
2004-05-12 16:26 ` Eljay Love-Jensen
@ 2004-05-12 18:08 ` llewelly
2004-05-12 18:43 ` paul moore
2 siblings, 1 reply; 7+ messages in thread
From: llewelly @ 2004-05-12 18:08 UTC (permalink / raw)
To: paul moore; +Cc: gcc-help
"paul moore" <paulmoore100@hotmail.com> writes:
> This code does not compile on gcc (3.2.2 on rh9 - 3.3 on suse 9)
>
> class test
> {
> public:
> int x;
> test(test &rhs)
> {
> x = rhx.x;
> }
> };
>
> test foo();
> int main(int argc, char* argv[])
> {
> test dd = foo();
C++ does not allow binding a temporary to a reference to non-const.
> return 0;
> }
>
> The test dd line gets hit saying there is no match for test::test(test), the
> candidates are test::test(test&)
> If I change the copy constructor to 'test(const test&)' it compiles fine.
>
> What am I trying to do? In real life the class contains an auto_ptr - since
> the auto_ptr copy constructor updates the rhs you cannot pass it in with
> const. (Note the auto_ptr copy constructor itself is declared with a non
> const rhs - and code using it compiles fine - this is really
> puzzling)
auto_ptr uses a truly hideous trick involving a sort of proxy class
called auto_ptr_ref. You are probably better off not tryiing to
emulate it. If you *need* to emulate it, you'll have to look at
the sources; I can't explain it.
>
> Also if I change foo to 'test &foo()' it compiles fine (but of course this
> is totally different semantics)
> Note also that MSFT vs 2003 compiles this code quite happily.
M$ supports binding a temporary to a reference to non-const as an extension.
^ permalink raw reply [flat|nested] 7+ messages in thread
* RE: Copy constructor with non const rhs arg
2004-05-12 18:08 ` llewelly
@ 2004-05-12 18:43 ` paul moore
0 siblings, 0 replies; 7+ messages in thread
From: paul moore @ 2004-05-12 18:43 UTC (permalink / raw)
To: llewelly; +Cc: gcc-help
Thanks to everybody for their input.
Using mutable worked a treat (first time I have used it in anger - you live
and learn)
-----Original Message-----
From: llewelly@xmission.xmission.com [mailto:llewelly@xmission.xmission.com]
On Behalf Of llewelly@xmission.com
Sent: Wednesday, May 12, 2004 11:09 AM
To: paul moore
Cc: gcc-help@gcc.gnu.org
Subject: Re: Copy constructor with non const rhs arg
"paul moore" <paulmoore100@hotmail.com> writes:
> This code does not compile on gcc (3.2.2 on rh9 - 3.3 on suse 9)
>
> class test
> {
> public:
> int x;
> test(test &rhs)
> {
> x = rhx.x;
> }
> };
>
> test foo();
> int main(int argc, char* argv[])
> {
> test dd = foo();
C++ does not allow binding a temporary to a reference to non-const.
> return 0;
> }
>
> The test dd line gets hit saying there is no match for
> test::test(test), the candidates are test::test(test&) If I change the
> copy constructor to 'test(const test&)' it compiles fine.
>
> What am I trying to do? In real life the class contains an auto_ptr -
> since the auto_ptr copy constructor updates the rhs you cannot pass it
> in with const. (Note the auto_ptr copy constructor itself is declared
> with a non const rhs - and code using it compiles fine - this is
> really
> puzzling)
auto_ptr uses a truly hideous trick involving a sort of proxy class
called auto_ptr_ref. You are probably better off not tryiing to
emulate it. If you *need* to emulate it, you'll have to look at
the sources; I can't explain it.
>
> Also if I change foo to 'test &foo()' it compiles fine (but of course
> this is totally different semantics) Note also that MSFT vs 2003
> compiles this code quite happily.
M$ supports binding a temporary to a reference to non-const as an extension.
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2004-05-12 18:43 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-05-12 15:06 Copy constructor with non const rhs arg paul moore
2004-05-12 16:05 ` Eljay Love-Jensen
2004-05-12 16:15 ` Eljay Love-Jensen
2004-05-12 16:43 ` Tony Wetmore
2004-05-12 16:26 ` Eljay Love-Jensen
2004-05-12 18:08 ` llewelly
2004-05-12 18:43 ` paul moore
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).