public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Sven Eschenberg <eschenb@cs.uni-frankfurt.de>
To: Mustafa4LP <mustafa.hussain@nechclst.in>,
	  GCC-help <gcc-help@gcc.gnu.org>
Subject: Re: Copy constructor not called.
Date: Wed, 12 Aug 2009 11:28:00 -0000	[thread overview]
Message-ID: <4A8264C2.5080509@cs.uni-frankfurt.de> (raw)
In-Reply-To: <24930356.post@talk.nabble.com>

As far as I can tell:

Your assignment Operator does not return a reference, that's why the 
compiler complains about not finding a matching copy contructor. Afaik 
semantically ( b = a ) would yield a copy constructor to create a 
temporary for the RV, which is A and not A & in your case. I don't know 
though, why a const in the copy constructor fixes the compile error as well.


And I want to quote from the man page:
        -fno-elide-constructors
            The C++ standard allows an implementation to omit creating a 
tempo-
            rary which is only used to initialize another object of the same
            type.  Specifying this option disables that optimization, and
            forces G++ to call the copy constructor in all cases.


As I read this this only relates to temporarys used for initialization. 
I don't see any initialization, only a RV temporary passed to the 
assignment operator. Thus the RVO will still be done and not be disabled 
by that option, as I read it.

Regards

-Sven


Mustafa4LP schrieb:
> Hi all,
> 
> Please consider the following program:
> 
> #include <iostream>
> using namespace std;
> class A
> {
> public:
>    A ()
>    {
>       cout << "constructor" << endl;
>    }
>    A (A &a)
>    {
>       cout << "copy constructor" << endl;
>    }
>    A operator = (const A &a)
>    {
>       cout << "= operator" << endl;
>    }
> };
> int main()
> {
>    A a;
>    A b;
>    b = ( b = a );
> }
> 
> 
> Following error is issued while running the above test case on gcc 3.4.6
> 
> test007.cpp: In function `int main()':
> test007.cpp:23: error: no matching function for call to `A::A(A)'
> test007.cpp:11: note: candidates are: A::A(A&)
> 
> However, if I change the argument of copy constructor to "const A &", the
> program compiles finely.
> I could not understand why const is required in copy constructor for running
> the test case.
> 
> Also note that in the output of the modified program (which compiled
> finely), no copy constructor is called.
> I am running the test case with --no-elide-constructors option to disable
> the RVO (so that I can get all copy constructor calls).
> 
> Thanks in advance for any help.
> 
> Regards,
> Mustafa 

  reply	other threads:[~2009-08-12  6:45 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-12 10:03 Mustafa4LP
2009-08-12 11:28 ` Sven Eschenberg [this message]
2009-08-12 20:04 ` John (Eljay) Love-Jensen
2009-08-13  9:28   ` Mustafa4LP

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4A8264C2.5080509@cs.uni-frankfurt.de \
    --to=eschenb@cs.uni-frankfurt.de \
    --cc=gcc-help@gcc.gnu.org \
    --cc=mustafa.hussain@nechclst.in \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).