c++/9748: c++ using "operator = ( const Foo& )" when it *should* use "operator = ( int )"

royce3@ev1.net royce3@ev1.net
Tue Feb 18 19:56:00 GMT 2003


>Number:         9748
>Category:       c++
>Synopsis:       c++ using "operator = ( const Foo& )" when it *should* use "operator = ( int )"
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Tue Feb 18 19:56:00 UTC 2003
>Closed-Date:
>Last-Modified:
>Originator:     royce3@ev1.net
>Release:        gcc version 3.2 (Mandrake Linux 9.0 3.2-1mdk)
>Organization:
>Environment:

>Description:
GCC is creating a temporary object and trying to use the
private copy constructor when there are several more appropriate selections.

$ g++ testcase.cpp
testcase.cpp: In function `int main()':
testcase.cpp:28: `Foo::Foo(const Foo&)' is private
testcase.cpp:39: within this context
testcase.cpp:39:   initializing temporary from result of `Foo::Foo(int)'
>How-To-Repeat:
$ g++ testcase.cpp

// testcase.cpp

#include <stdio.h>

class Foo
{
        int _i;
public:
        Foo() : _i(0)
        {
        }
        Foo ( int i ) : _i(i)
        {
        }
        const Foo& operator = ( int i )
        {
                _i = i;
                return *this;
        }
//      operator int() const
//      {
//              return _i;
//      }

private:
// disable copy semantics
        Foo ( const Foo& ) { }
        const Foo& operator = ( const Foo& ) { return *this; }
};

int returnsInt()
{
        return 7;
}

int main()
{
        Foo foo = returnsInt();
}

// end of testcase.cpp
>Fix:
If GCC has an option to use a function which does not require the creation of a temporary object, it should do so.
>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list