This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/18113] compiler allows 2 copy constructors


------- Additional Comments From reichelt at gcc dot gnu dot org  2004-10-26 20:34 -------
Here's a detailed explanation of the second example:

You are calling the function "void HandleObject(TObject toj);" as follows:
"HandleObject( GetAnObject() );"
To do this you need a copy constructor to copy the result of
"GetAnObject()" into "toj".

The only copy constructor that is available is "TObject(const rObject)"
which is essentially "TObject(TObject&)" and not "TObject(const TObject&)"
as one might think (see comment #2).

However, "TObject(TObject&)" can only take an lvalue (something that can
be modified) as argument, but "GetAnObject()" only provides an lvalue
(which cannot be modified). Therefore, gcc correctly complains that it
cannot find a suitable copy constructor.

This is a bug in your code and not in gcc.

Note, that the compiler is allowed to optimize away the copy constructor.
Nevertheless, it has to check whether the copy constructor is present.
Alas, many compilers forgot to check this.


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=18113


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]