Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Overload re...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 11 Topic 45791 of 47032
Post > Topic >>

Re: Overload resolution and copy constructors that take non-const references

by "Victor Bazarov" <v.Abazarov@[EMAIL PROTECTED] > May 7, 2008 at 08:59 AM

Branimir Maksimovic wrote:
> ....
> It isn;t clear to me following example:
>
> class A{
> public:
>   A(){}
>   A(A&){}
> };
>
> A foo(){ return A(); }
>
> int main()
> {
>   A a;
>   a = foo();
> }
>
> g++ gives strange error:
>
> cctor.cpp: In function ‘A foo()’:
> cctor.cpp:7: error: no matching function for call to ‘A::A(A)’
> cctor.cpp:4: note: candidates are: A::A(A&)
> cctor.cpp: In function ‘int main()’:
> cctor.cpp:12: error: no matching function for call to ‘A::A(A)’
> 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).  But the only copy c-tor in 'A' takes
a ref to non-const A.  The ref argument for 'A(A&)' cannot be
bound to the tem****ary, and that would probably be a clearer
message...

>
> I don;t have msvc handy but I think saw somewhere,
> that, it compiles without error.

... unless you disable language extensions ..

> So, perhaps, as g++ wants A(A) this is similar to
> your question.
> As I never used non const reference copy constructor,
> I'm really confused about it's behavior.
> It seems to me that compiler writers are too ;)

I don't believe that's the case (about the writers).

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 




 11 Posts in Topic:
Overload resolution and copy constructors that take non-const re
Anthony Williams <anth  2008-05-07 10:44:48 
Re: Overload resolution and copy constructors that take non-cons
Branimir Maksimovic <b  2008-05-07 05:54:26 
Re: Overload resolution and copy constructors that take non-cons
"Victor Bazarov"  2008-05-07 08:59:18 
Re: Overload resolution and copy constructors that take non-cons
Branimir Maksimovic <b  2008-05-07 06:21:21 
Re: Overload resolution and copy constructors that take non-cons
Branimir Maksimovic <b  2008-05-07 06:40:00 
Re: Overload resolution and copy constructors that take non-cons
"Victor Bazarov"  2008-05-07 10:25:23 
Re: Overload resolution and copy constructors that take non-cons
dizzy <dizzy@[EMAIL PR  2008-05-07 17:47:00 
Re: Overload resolution and copy constructors that take non-cons
"Victor Bazarov"  2008-05-07 11:33:53 
Re: Overload resolution and copy constructors that take non-cons
Branimir Maksimovic <b  2008-05-07 15:18:46 
Re: Overload resolution and copy constructors that take non-cons
"Alf P. Steinbach&qu  2008-05-08 00:34:33 
Re: Overload resolution and copy constructors that take non-cons
James Kanze <james.kan  2008-05-09 03:28:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:16:58 CDT 2008.