On May 7, 2:59=A0pm, "Victor Bazarov" <v.Abaza...@[EMAIL PROTECTED]
> wrote:
> Branimir Maksimovic wrote:
> > ....
> > It isn;t clear to me following example:
>
> > class A{
> > public:
> > =A0 A(){}
> > =A0 A(A&){}
> > };
>
> > A foo(){ return A(); }
>
> > int main()
> > {
> > =A0 A a;
> > =A0 a =3D foo();
> > }
>
> > g++ gives strange error:
>
> > cctor.cpp: In function =91A foo()=92:
> > cctor.cpp:7: error: no matching function for call to =91A::A(A)=92
> > cctor.cpp:4: note: candidates are: A::A(A&)
> > cctor.cpp: In function =91int main()=92:
> > cctor.cpp:12: error: no matching function for call to =91A::A(A)=92
> > cctor.cpp:4: note: candidates are: A::A(A&)
>
> Returning a tem****ary from 'foo' requires copy-constructing
> another tem****ary (allowed to be optimized away, but semantially
> should still be possible). =A0But the only copy c-tor in 'A' takes
> a ref to non-const A. =A0The ref argument for 'A(A&)' cannot be
> bound to the tem****ary, and that would probably be a clearer
> message...
I don;t think that's the case since g++ gives proper
error if, for example, ordinary function foo takes non const reference
to tem****ary.In this case g++ really wants A(A),
I think. But then again, you may be right, it's just
strange error.
What about
A foo()
{
A a;
return a;
}
A a;
a =3D foo();
Is compiler obliged to call copy constructor?
>
>
>
> > I don;t have msvc handy but I think saw somewhere,
> > that, it compiles without error.
>
> .. unless you disable language extensions ..
I think that msvc issues warning if that's the case.
But I am really not sure, could be what you say,
so someone with msvc with disabled extensions
could confirm that this does not compile (second version
of foo that does not returns tem****ary perhaps).
Greetings, Branimir.


|