473,406 Members | 2,369 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,406 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 1545
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.