By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,516 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

confusion between copy and templated constructors

P: n/a
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
Jun 22 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 22 Jun 2007 12:49:34 +0100, Richard Thompson <rj***@yaho.com>
wrote:
>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
Jun 22 '07 #2

P: n/a
Richard Thompson wrote:
On Fri, 22 Jun 2007 12:49:34 +0100, Richard Thompson <rj***@yaho.com>
wrote:
>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);
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 22 '07 #3

P: n/a
On 2007-06-22 04:49:34 -0700, Richard Thompson <rj***@yaho.comsaid:
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.

--
Clark S. Cox III
cl*******@gmail.com

Jun 22 '07 #4

P: n/a
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Richard Thompson wrote:
>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?
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
Jun 22 '07 #5

P: n/a
On 22 Jun, 17:12, Richard Thompson <r...@yaho.comwrote:
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"

<v.Abaza...@comAcast.netwrote:
Richard Thompson wrote:
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?

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

Jun 22 '07 #6

P: n/a
On 2007-06-22 09:12:14 -0700, Richard Thompson <rj***@yaho.comsaid:
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Richard Thompson wrote:
>>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?

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).

--
Clark S. Cox III
cl*******@gmail.com

Jun 22 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.