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]

c++/2167: wrong constructor called



>Number:         2167
>Category:       c++
>Synopsis:       wrong constructor called
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          rejects-legal
>Submitter-Id:   net
>Arrival-Date:   Sat Mar 03 22:26:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Kenny Simpson
>Release:        gcc version 3.0 20010303 (prerelease)
>Organization:
>Environment:
GCC Online Compiler Submission (March 03, 2001)
>Description:
Last month a bug was reported, but I have seen no responce,
so I am putting this into GNATS so that it does not get
forgotten.
http://gcc.gnu.org/ml/gcc-bugs/2001-02/msg00495.html

The implicit copy constructor is not calling the correct
base constructor.
>How-To-Repeat:
Compile the following:

struct B
{
  B() {}
  B(B const&) {} // works if this line is commented out

  template<typename T>
  B(T const& a) { a = a + a; }
};

struct D : B {};

int main()
{
  D d;
  D d2(d); // calls templated base constructor - bad
  return 0;
}


The compiler spews:
     /tmp/@16553.7.cc: In constructor `B::B(const T&) [with T = D]':
     /tmp/@16553.7.cc:15:   instantiated from here
     /tmp/@16553.7.cc:7: no match for `const D& + const D&' operator

Line 15 is the copy construction of d2 which should not be
instantiating B's template constructor.
>Fix:
>From msg00495:

The only work-around I've found is to explicitly provide the copy
constructor, with a static_cast of the reference argument to the base
class type, e.g.:

    D::D( D const& other )
        :   B( static_cast< B const& >( other ) )
    {
    }

>Release-Note:
>Audit-Trail:
>Unformatted:


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