473,215 Members | 1,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,215 software developers and data experts.

confusion between copy and templated constructors

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
6 1536
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
4
by: Don Kim | last post by:
I have the following code: using namespace System; ref class R { public: R() {
3
by: subramanian | last post by:
Consider the code fragment: class Test { public: Test(const Test &temp); ... }; ....
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.