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<typena me T>
MyClass(T initval);
// ctor 2
template<typena me 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 6 1567
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
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
On 2007-06-22 04:49:34 -0700, Richard Thompson <rj***@yaho.com said:
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
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"
<v.********@com Acast.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
On 22 Jun, 17:12, Richard Thompson <r...@yaho.comw rote:
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"
<v.Abaza...@com Acast.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
On 2007-06-22 09:12:14 -0700, Richard Thompson <rj***@yaho.com said:
On Fri, 22 Jun 2007 09:16:03 -0400, "Victor Bazarov"
<v.********@com Acast.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 kind. It sounds simple
but evidently .NET has difficulty with this concept for some reason. I do
understand that .NET objects are created on the GC heap but that doesn't
mean that they couldn't be copied from another object of the same kind when...
|
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 my_template
|
by: Don Kim |
last post by:
I have the following code:
using namespace System;
ref class R
{
public:
R()
{
|
by: subramanian |
last post by:
Consider the code fragment:
class Test {
public:
Test(const Test &temp);
...
};
....
|
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, short Trank, bool Tcont>
TopTen<Tnum,Trank,Tcont>::TopTen( const TopTen<Tnum,Trank,Tcont&
tten );
| |
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |