public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
From: mike_harris@filemaker.com
To: gcc-gnats@gcc.gnu.org
Cc: kristof_subryan@filemaker.com
Subject: c++/5937: g++ 3.0.2 does not allow return of class constructor
Date: Wed, 13 Mar 2002 09:16:00 -0000	[thread overview]
Message-ID: <20020313171058.9297.qmail@sources.redhat.com> (raw)


>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=


             reply	other threads:[~2002-03-13 17:16 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-03-13  9:16 mike_harris [this message]
2002-04-25  6:46 nathan

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=20020313171058.9297.qmail@sources.redhat.com \
    --to=mike_harris@filemaker.com \
    --cc=gcc-gnats@gcc.gnu.org \
    --cc=kristof_subryan@filemaker.com \
    /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).