Re: Why does the copy constructor get called?

Discussion in 'C++' started by Tom Widmer, Nov 17, 2004.

  1. Tom Widmer

    Tom Widmer Guest

    On Tue, 16 Nov 2004 14:32:12 -0500, bala <> wrote:

    >I must have made a mistake in posting the last time as I did not see my
    >posting for a while. My apologies for the duplicate posting.
    >
    >Anyway, I am not sure why g++ 3.4 is trying to invoke the copy
    >constructor in line 11 below.
    >
    >// Here is the code tst.C
    >struct A {
    > explicit A(A const&) {}
    > A(int) {}
    >};
    >void foo(A const&) {}
    >int main() {
    > A a(1);
    > foo(a); // no complaints here (as expected)
    > foo(A(2)); // line 11
    >}
    >
    >When compiling under g++3.4, I get the following: tst.C:11: no matching
    >function for call to `A::A(const A&)'.
    >
    >A() is not explicit, so a temporary can be easily created and then
    >passed on to foo(). It looks like the compiler is attempting to copy the
    >created temporary into another temporary (which of course would violate
    >the explicit copy ctor).
    >
    >In 12.2 in the standard (p213), there is a similar example but their
    >function f() is declared slightly differently: 'X f(X);' and then calls
    >'X b = f(X(2));' In the comments, it says, "Here an implementation might
    >use a temporary in which to construct X(2) before passing it to f()
    >using X's copy ctor, ..."
    >
    >Now, in my example, there is no need for a copy ctor because foo() takes
    >a reference to a const as its argument. Is this a problem with the over
    >zealous interpretation of the rules by the compiler? Or, am I
    >interpreting the rules wrongly?.


    I think the problem is that when binding an rvalue to a const
    reference, the compiler is permitted to generate a copy of that rvalue
    using a constructor (it is implementation defined). See 8.5.3/5.

    Tom
     
    Tom Widmer, Nov 17, 2004
    #1
    1. Advertising

  2. Tom Widmer wrote:
    > On Tue, 16 Nov 2004 14:32:12 -0500, bala <> wrote:
    >
    >
    >>I must have made a mistake in posting the last time as I did not see my
    >>posting for a while. My apologies for the duplicate posting.
    >>
    >>Anyway, I am not sure why g++ 3.4 is trying to invoke the copy
    >>constructor in line 11 below.
    >>
    >>// Here is the code tst.C
    >>struct A {
    >>explicit A(A const&) {}
    >>A(int) {}
    >>};
    >>void foo(A const&) {}
    >>int main() {
    >>A a(1);
    >>foo(a); // no complaints here (as expected)
    >>foo(A(2)); // line 11
    >>}
    >>
    >>When compiling under g++3.4, I get the following: tst.C:11: no matching
    >>function for call to `A::A(const A&)'.
    >>
    >>A() is not explicit, so a temporary can be easily created and then
    >>passed on to foo(). It looks like the compiler is attempting to copy the
    >>created temporary into another temporary (which of course would violate
    >>the explicit copy ctor).
    >>
    >>In 12.2 in the standard (p213), there is a similar example but their
    >>function f() is declared slightly differently: 'X f(X);' and then calls
    >>'X b = f(X(2));' In the comments, it says, "Here an implementation might
    >>use a temporary in which to construct X(2) before passing it to f()
    >>using X's copy ctor, ..."
    >>
    >>Now, in my example, there is no need for a copy ctor because foo() takes
    >>a reference to a const as its argument. Is this a problem with the over
    >>zealous interpretation of the rules by the compiler? Or, am I
    >>interpreting the rules wrongly?.

    >
    >
    > I think the problem is that when binding an rvalue to a const
    > reference, the compiler is permitted to generate a copy of that rvalue
    > using a constructor (it is implementation defined). See 8.5.3/5.


    Just to clarify...

    The compiler is also permitted not to generate the copy. However, the
    copy c-tor _must_ be callable _as_if_ a copy is generated. Since the
    copy c-tor here is explicit, it cannot be used in that situation where
    the compiler needs an implicit copy.

    V
     
    Victor Bazarov, Nov 17, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Apricot
    Replies:
    4
    Views:
    532
    velthuijsen
    Apr 16, 2004
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,006
    Smokey Grindel
    Dec 2, 2006
  3. TechCrazy
    Replies:
    5
    Views:
    398
    Karl Heinz Buchegger
    Jul 15, 2005
  4. Replies:
    9
    Views:
    1,846
    Alf P. Steinbach
    Oct 14, 2006
  5. cinsk
    Replies:
    35
    Views:
    2,616
    James Kanze
    Oct 11, 2010
Loading...

Share This Page