R
Richard Thompson
Hi -
I have a program which was previously working (but wasn't well
tested). I've added a new function call, and it now doesn't compile.
The call requires a copy constructor, but the compiler appears to
think that it actually calls one of the *other* constructors. The copy
ctor would work in this context, but the other ctor can't be used (it
leads to a template instantiation error, which the compiler is
reporting).
The new code is:
const MyClass foo(a, b, c);
....
const myClass bar = foo(x, y, z); // *not* using copy ctor!!
So, my question is - have I got my ctors wrong? Have I misunderstood
the usage of templated ctors? Why would the compiler think that my
copy ctor is not the one that I think it is?
My ctors are:
// intended to be the copy ctor
MyClass(const MyClass& src);
// copy assignment
MyClass& operator= (const MyClass& src);
// ctor 1: the one gcc is using in the case above: why??
template<typename T>
MyClass(T initval);
// ctor 2
template<typename T>
MyClass(T initval, int nbits_);
// ctor 3: a specific non-templated version of ctor 2
MyClass(const MyClass& initval, int nbits_);
Many thanks -
Richard
I have a program which was previously working (but wasn't well
tested). I've added a new function call, and it now doesn't compile.
The call requires a copy constructor, but the compiler appears to
think that it actually calls one of the *other* constructors. The copy
ctor would work in this context, but the other ctor can't be used (it
leads to a template instantiation error, which the compiler is
reporting).
The new code is:
const MyClass foo(a, b, c);
....
const myClass bar = foo(x, y, z); // *not* using copy ctor!!
So, my question is - have I got my ctors wrong? Have I misunderstood
the usage of templated ctors? Why would the compiler think that my
copy ctor is not the one that I think it is?
My ctors are:
// intended to be the copy ctor
MyClass(const MyClass& src);
// copy assignment
MyClass& operator= (const MyClass& src);
// ctor 1: the one gcc is using in the case above: why??
template<typename T>
MyClass(T initval);
// ctor 2
template<typename T>
MyClass(T initval, int nbits_);
// ctor 3: a specific non-templated version of ctor 2
MyClass(const MyClass& initval, int nbits_);
Many thanks -
Richard