confusion between copy and templated constructors

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
 
R

Richard Thompson

The new code is:

const MyClass foo(a, b, c);
...
const myClass bar = foo(x, y, z); // *not* using copy ctor!!

Sorry - being dumb. In more detail, this should have been:

const myClass foo(const MyClass& a, const MyClass &b, etc);
....
void wibble(const MyClass* a, const MyClass* b) {
const MyClass bar = foo(a, b, etc);
...
}

So, I was passing ptrs to 'foo', rather than refs. I'm not quite sure
what happens next; I don't think a copy ctor is required when passing
the args to 'foo', since reference args are specified. However, at
some point the MyClass ptrs seem to go through ctor 2 to turn them
into MyClass objects, and this seems to be where the failure was.
Changing the 'foo' call to "foo(*a, *b, etc)" fixes the problem.

Thanks -

Richard
 
V

Victor Bazarov

Richard said:
Sorry - being dumb. In more detail, this should have been:

const myClass foo(const MyClass& a, const MyClass &b, etc);

You're doing it again! 'myClass' as the return value type: is it the
same as the type of the references passed in or is it different?
...
void wibble(const MyClass* a, const MyClass* b) {
const MyClass bar = foo(a, b, etc);
...
}

So, I was passing ptrs to 'foo', rather than refs. I'm not quite sure
what happens next; I don't think a copy ctor is required when passing
the args to 'foo', since reference args are specified. However, at
some point the MyClass ptrs seem to go through ctor 2 to turn them
into MyClass objects, and this seems to be where the failure was.
Changing the 'foo' call to "foo(*a, *b, etc)" fixes the problem.

Since 'foo' expects a reference to const 'MyClass', the compiler creates
a temporary object of type 'MyClass' and binds the reference to it. The
temporary object is created using the (you guessed it!) templated c-tor
with (T == MyClass const*).

V
 
C

Clark Cox

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!!

Well, it could be using return value optimization, and just skipping
the copy altogether.
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?

Post some actual code (i.e. that can be compiled and run). Without
that, you're asking people to be mind readers.
 
R

Richard Thompson

Richard Thompson wrote:

You're doing it again! 'myClass' as the return value type: is it the
same as the type of the references passed in or is it different?

It's the same - there's a typo in my capitaliastion; I was trying to
cut out lots of unnecessary additional bits, rather than post a
compilable example.
Since 'foo' expects a reference to const 'MyClass', the compiler creates
a temporary object of type 'MyClass' and binds the reference to it. The
temporary object is created using the (you guessed it!) templated c-tor
with (T == MyClass const*).

Thanks -

Richard
 
G

Gavin Deane

It's the same - there's a typo in my capitaliastion; I was trying to
cut out lots of unnecessary additional bits, rather than post a
compilable example.

Cut out the unnecessary bits *in your code editor*, compile it
yourself, run it yourself to confirm the behaviour you are questioning
is still exhibited, *then* copy and paste directly from your code
editor into your message to post it here. That will generate the
complete, minimal, compileable example as per the posting guidelines
in the FAQ. A handy side benefit is that, as you go through the cycle
of

remove unnecessary bits
compile
run and check the behaviour
repeat until minimal, complete, compileable example produced

you might find the problem goes away after a particular iteration of
the cycle and so you know that whatever unnecessary bit you last
removed was actually the cause of the problem.

Victor spotted the typo this time and guessed what you really meant.
With the best will in the world, if you type code straight into your
post instead of copying and pasting from your code editor, you will
occasionally introduce typos. And the next one may not be so easy to
spot or correct, or, worse could lead anyone trying to answer your
question down the wrong track, wasting their tima and yours.

Gavin Deane
 
C

Clark Cox

It's the same - there's a typo in my capitaliastion; I was trying to
cut out lots of unnecessary additional bits, rather than post a
compilable example.

Do both (i.e. post an example with the unnecessary bits cut out that is
*itself* compilable).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top