c++/5937: g++ 3.0.2 does not allow return of class constructor
mike_harris@filemaker.com
mike_harris@filemaker.com
Wed Mar 13 09:16:00 GMT 2002
>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=
More information about the Gcc-prs
mailing list