473,624 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator = defined using template?

I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).

Adding explicit operator = helped. See code snip below.

I am not sure if this compiler behaviour is standard conformant or not. Any
thoughts?

Regards
Ondrej

--
---------------------------------------
Ondrej Spanel
Lead Programmer
Bohemia Interactive Studio
www.bistudio.com
www.flashpoint1985.com

----------------------------------------------------------------------------
---

template<class Type, class Allocator=MemAl locD>
class AutoArray
{
Type *_data;
int _size;
....
public:
/// template "operator ="
template <class AnotherAllocato r>
void operator = ( const AutoArray<Type, AnotherAllocato r> &src )
{
... deep copy of _data array
}

/// explicit "operator =" - class does not compile well without it
void operator = ( const AutoArray &src )
{
... deep copy of _data array
}
};

void Foo()
{
AutoArray< SomeTemplate<So meClass> > a;
AutoArray< SomeTemplate<So meClass> > b;

b = a; /// bad code generated when explicit "operator =" not defined.
}
Nov 16 '05 #1
3 3400
Ondrej Spanel wrote:
I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).

Adding explicit operator = helped. See code snip below.

I am not sure if this compiler behaviour is standard conformant or not. Any
thoughts?


According to the standard, a template assignment operator is never a copy
assignment operator and doesn't suppress generation of one by the compiler.
(Ditto for copy ctors.) So it sounds like the compiler was doing the right
thing, and you solved your problem in the right way.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 16 '05 #2
"Ondrej Spanel" <on***********@ bistudionospam. com> wrote in message news:<eY******* *******@TK2MSFT NGP09.phx.gbl>. ..
I defined operator = using template member function for a template class.
However compiler failed to recognize it is defined and created its own
version (which was member-wise copy, and a result was disastrous and hard to
debug bug, as pointer was copied).


This is standard conforming behavior. The compiler will generate a
copy assignment operator if it does not find a user-defined
non-template copy assignment operator (cf. 12.8/9 footnote 109):

"Because a template assignment operator is never a copy assignment
operator, the presence of such a template does not suppress the
implicit declaration of a copy assignment operator. Template
assignment operators participate in overload resolution with other
assignment operators, including copy assignment operators, and a
template assignment operator may be used to assign an object if it
provides a better match than other assignment operators."

John
Nov 16 '05 #3
> >I am not sure if this compiler behaviour is standard conformant or not.
Any
thoughts?
According to the standard, a template assignment operator is never a copy
assignment operator and doesn't suppress generation of one by the

compiler. (Ditto for copy ctors.) So it sounds like the compiler was doing the right
thing, and you solved your problem in the right way.


Thank you for your explanation.

Regards
Ondrej
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
1708
by: ES Kim | last post by:
Consider: #include <string> #include <iostream> using namespace std; struct S { const char* ps_; operator string();
11
912
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have nice and working allocation deallocation routines. However, I don't want to loose the nice extras of new operator, like - constructor calling, typecasting the result, keeping the array size, etc. For another bunch of reasons, outside this scope I...
5
3026
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a templatized conversion operator defined as follows:
13
1739
by: Atlas | last post by:
Hi, I implemented a template as: template <int L, int M, int T> class Quantity { ..... public: friend Quantity operator*(const Quantity& q1,const Quantity& q2); ..... };
4
2495
by: Amadeus W. M. | last post by:
What is the difference between friend ostream & operator<<(ostream & OUT, const Foo & f){ // output f return OUT; } and template <class X>
2
2912
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class ("base"). I want to be able to add: 1) any derived array holding class to any other derived array holding class 2) any derived array holding class to a literal value (e.g int, double, etc) for which addition is defined for the type in the...
7
1457
by: Jim Langston | last post by:
What I want to do is have an operator= accept a template variable. I will have some classes which all will contain an instance of a different class. I want an operator= in yet a 3rd class to accept these classes and use the instance. This is confusing as heck, so here's kinda what I want to do: class COffsetMap { public:
9
2052
by: wo3kie | last post by:
#include <iostream> #include <map> #include <utility> // // Base // / | \ // Derived1 Derived2 \ // \ | / // Derived3
2
1782
by: Peng Yu | last post by:
Hi, In the following code, the 'copy' member function works. But the '=' operator does not work. Can somebody let me know why a member function is different from an operator. Thanks, Peng #include <iostream>
0
8236
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, 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...
0
8173
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,...
0
8679
Oralloy
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...
0
8621
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 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...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7159
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
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...
1
2606
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.