public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* Re: c++/5937: g++ 3.0.2 does not allow return of class constructor
@ 2002-04-25  6:46 nathan
  0 siblings, 0 replies; 2+ messages in thread
From: nathan @ 2002-04-25  6:46 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, kristof_subryan, mike_harris, nobody

Synopsis: g++ 3.0.2 does not allow return of class constructor

State-Changed-From-To: open->closed
State-Changed-By: nathan
State-Changed-When: Thu Apr 25 06:46:18 2002
State-Changed-Why:
    Not a bug. 
    	return X(d)
    uses the X::X(int) ctor to create a temporary, and then
    requires a copy constructor to copy that temporary
    into the return slot. See [6.6.3]/2 and [12.2]. Now, we
    can (and do) eliminate that temporary, as allowed by
    [12.8]/15, but that is an optimization and the copy
    ctor must still be accessible.

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=5937


^ permalink raw reply	[flat|nested] 2+ messages in thread

* c++/5937: g++ 3.0.2 does not allow return of class constructor
@ 2002-03-13  9:16 mike_harris
  0 siblings, 0 replies; 2+ messages in thread
From: mike_harris @ 2002-03-13  9:16 UTC (permalink / raw)
  To: gcc-gnats; +Cc: kristof_subryan


>Number:         5937
>Category:       c++
>Synopsis:       g++ 3.0.2 does not allow return of class constructor
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Wed Mar 13 09:16:01 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Mike Harris
>Release:        gcc version 3.0.2
>Organization:
>Environment:
GNU/Linux 2.4.3 (Intel i686)
Reading specs from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.0.2/specs
Configured with: ../gcc-3.0.2/configure --prefix=/usr --enable-shared --enable-threads
Thread model: posix
gcc version 3.0.2
>Description:
g++ will not allow a return of a class constructor which is marked explicit without the class copy constructor public.   I do not want uncessary copies of the class to occur, therefore, the copy constructor and assignment operator are private.  I want all constructions to be explicit, which "return X(d);" is. 

Interestingly, when the copy constructor is public, the code compiles, however, the when it is run, the copy constructor is never called. 

Compiler Output:

[lotus@hyperion trial]$ g++ -o explicit_test explicit_test.cpp
explicit_test.cpp: In function `X f(int)':
explicit_test.cpp:15: `X::X(const X&)' is private
explicit_test.cpp:19: within this context
explicit_test.cpp: In function `int main()':
explicit_test.cpp:15: `X::X(const X&)' is private
explicit_test.cpp:23: within this context
>How-To-Repeat:
(not sure if Galeon supports file upload...)

#include <iostream>

class X 
{
public:

	explicit X(int d) : mData(d) {  }
	~X( ) { };
	
	int mData;

private:

	X( const X& ) { std::cout << "Copy called\n"; }
	X& operator=( const X& ) { }
};

X f(int d) { return X(d); }

int main( void )
{
	X tmpX = f(10);

	std::cout << "tmpX: " << tmpX.mData << std::endl; 

	return 0;
}
>Fix:
The pre-processor (or whichever component it really is) should not complain about not having a copy constructor when returning a class constructor.
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/cpp; name="explicit_test.cpp"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="explicit_test.cpp"

CiNpbmNsdWRlIDxpb3N0cmVhbT4KCmNsYXNzIFggCnsKcHVibGljOgoKCWV4cGxpY2l0IFgoaW50
IGQpIDogbURhdGEoZCkgeyAgfQoJflgoICkgeyB9OwoJCglpbnQgbURhdGE7Cgpwcml2YXRlOgoK
CVgoIGNvbnN0IFgmICkgeyBzdGQ6OmNvdXQgPDwgIkNvcHkgY2FsbGVkXG4iOyB9CglYJiBvcGVy
YXRvcj0oIGNvbnN0IFgmICkgeyB9Cn07CgpYIGYoaW50IGQpIHsgcmV0dXJuIFgoZCk7IH0KCmlu
dCBtYWluKCB2b2lkICkKewoJWCB0bXBYID0gZigxMCk7CgoJc3RkOjpjb3V0IDw8ICJ0bXBYOiAi
IDw8IHRtcFgubURhdGEgPDwgc3RkOjplbmRsOyAKCglyZXR1cm4gMDsKfQo=


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2002-04-25 13:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-25  6:46 c++/5937: g++ 3.0.2 does not allow return of class constructor nathan
  -- strict thread matches above, loose matches on Subject: below --
2002-03-13  9:16 mike_harris

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).