473,795 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

operator= in allocators

I'm having yet another headache with making a standard allocator. What
behaviour should be expected from the assigment operator? When would it
be called? Why is it there at all, when you can't change allocators in
containers?

To give a little more depth, I have a class like this:

class HeapAllocator
{
// All requirements of the allocator interface are met along with:
// - A constructor to pass in a heap reference
// - A GetHeap function that returns a reference to the internal heap
// (which can never be 0)

private:
mutable Heap* heap_;
};

Now, I would have liked to define heap_ as a Heap&, but I can't because of:

template <class U>
HeapAllocator& operator=(HeapA llocator<U> const& ha) /*throw()*/
{
heap_ = ha.heap_;
}

Which is an annoyance but otherwise not that big of an issue. The
problem for me is one of elegance and robustness. Under what
circumstances could I expect operator= to be called (aside from in
operator= in a container)? Memory allocated in one heap cannot be
deallocated in another (operator== tests for heap equality). Am I
guaranteed that all allocations would be deallocated by the correct
allocator (or to put it more specifically allocations by an allocator A
will only be deallocated by A or by another allocator B for which B ==
A) - ie, is this a requirement?

I'm a little disturbed by the statement in 20.1.5.4:

"Implementation s of containers described in this International Standard
are permitted to assume that their Allocator template parameter meets
the following two additional requirements... .

- All instances of a given allocator type are required to be
interchangeable and always compare equal to each other."

Does this mean that I am essentially wasting my time?

mark

Jul 22 '05 #1
18 2227
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote
I'm having yet another headache with making a
standard allocator. What behaviour should be
expected from the assigment operator? When
would it be called? Why is it there at all, when
you can't change allocators in containers?

[rest snipped]


What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and
don't implement them. Not all objects should have value semantics, and
allocators are a prime example.

Claudio Puviani
Jul 22 '05 #2


Claudio Puviani wrote:
What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and
don't implement them. Not all objects should have value semantics, and
allocators are a prime example.


I agree, that's actually the basis for my question. Aren't operator= and
the copy constructor required for the allocator interface?

mark

Jul 22 '05 #3
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote
I'm having yet another headache with making a
standard allocator. What behaviour should be
expected from the assigment operator? When
would it be called? Why is it there at all, when
you can't change allocators in containers?

[rest snipped]


What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and
don't implement them. Not all objects should have value semantics, and
allocators are a prime example.

Claudio Puviani
Jul 22 '05 #4


Claudio Puviani wrote:
What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and
don't implement them. Not all objects should have value semantics, and
allocators are a prime example.


I agree, that's actually the basis for my question. Aren't operator= and
the copy constructor required for the allocator interface?

mark

Jul 22 '05 #5
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote
Claudio Puviani wrote:
What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and don't implement them. Not all objects should have value semantics, and
allocators are a prime example.


I agree, that's actually the basis for my question. Aren't operator= and
the copy constructor required for the allocator interface?


20.1.5 ("Allocator requirements") doesn't state copyability as a
requirement. Comparison, yes, but definitely not copying.

Claudio Puviani
Jul 22 '05 #6
Claudio Puviani wrote:
20.1.5 ("Allocator requirements") doesn't state copyability as a
requirement. Comparison, yes, but definitely not copying.


But that makes no logical sense if containers own their allocator.

vector<int, SomeAllocator> foo()
{
SomeAllocator a;
vector<int, SomeAllocator> v(a);
return v;
}

void bar()
{
vector<int, SomeAllocator> v = foo();
// Undefined behaviour
}

And how can the containers not own their own allocator?

mark

Jul 22 '05 #7
In article
<De************ *******@news01. bloor.is.net.ca ble.rogers.com> ,
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote:
I'm having yet another headache with making a standard allocator. What
behaviour should be expected from the assigment operator? When would it
be called? Why is it there at all, when you can't change allocators in
containers?

To give a little more depth, I have a class like this:

class HeapAllocator
{
// All requirements of the allocator interface are met along with:
// - A constructor to pass in a heap reference
// - A GetHeap function that returns a reference to the internal heap
// (which can never be 0)

private:
mutable Heap* heap_;
};

Now, I would have liked to define heap_ as a Heap&, but I can't because of:

template <class U>
HeapAllocator& operator=(HeapA llocator<U> const& ha) /*throw()*/
{
heap_ = ha.heap_;
}

Which is an annoyance but otherwise not that big of an issue. The
problem for me is one of elegance and robustness. Under what
circumstances could I expect operator= to be called (aside from in
operator= in a container)? Memory allocated in one heap cannot be
deallocated in another (operator== tests for heap equality). Am I
guaranteed that all allocations would be deallocated by the correct
allocator (or to put it more specifically allocations by an allocator A
will only be deallocated by A or by another allocator B for which B ==
A) - ie, is this a requirement?

I'm a little disturbed by the statement in 20.1.5.4:

"Implementation s of containers described in this International Standard
are permitted to assume that their Allocator template parameter meets
the following two additional requirements... .

- All instances of a given allocator type are required to be
interchangeable and always compare equal to each other."

Does this mean that I am essentially wasting my time?


Not at all. But it does mean you have stumbled into implementation
defined territory. Your experience could contribute to the next C++
standard.

Allocators are not required to be assignable, so you could just omit the
operator=. However, allocators are required to be equal. But however
again, implementors are encouraged to deal with non-equal allocators
(and several do).

Non-equal allocators mainly come into play during resource transferring
operations such as swap or splice, but not during container assignment.
There is an open lwg issue concerning swap here:

http://anubis.dkuug.dk/jtc1/sc22/wg2...ctive.html#431

Here is a response I have written to that issue:

http://anubis.dkuug.dk/jtc1/sc22/wg2...004/n1599.html

Neither the issue, nor the response have yet been discussed in
committee. I point them out just fyi, and also so that you have the
opportunity to join in the discussion.

-Howard
Jul 22 '05 #8
"Mark A. Gibbs" <x_*********@ro gers.com_x> wrote
Claudio Puviani wrote:
What does copying an allocator mean to you? If your answer is, "it's
meaningless" -- as I hope it is -- then you shouldn't provide coping
operations. Just declare the copy constructor and operator= as private and don't implement them. Not all objects should have value semantics, and
allocators are a prime example.


I agree, that's actually the basis for my question. Aren't operator= and
the copy constructor required for the allocator interface?


20.1.5 ("Allocator requirements") doesn't state copyability as a
requirement. Comparison, yes, but definitely not copying.

Claudio Puviani
Jul 22 '05 #9
Claudio Puviani wrote:
20.1.5 ("Allocator requirements") doesn't state copyability as a
requirement. Comparison, yes, but definitely not copying.


But that makes no logical sense if containers own their allocator.

vector<int, SomeAllocator> foo()
{
SomeAllocator a;
vector<int, SomeAllocator> v(a);
return v;
}

void bar()
{
vector<int, SomeAllocator> v = foo();
// Undefined behaviour
}

And how can the containers not own their own allocator?

mark

Jul 22 '05 #10

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

Similar topics

1
2097
by: Dodo | last post by:
I have overloaded the global new/delete operators with something like this (simplified): void *operator new(size_t size) { ...allocation code... } void operator delete(void * p) {
18
380
by: Mark A. Gibbs | last post by:
I'm having yet another headache with making a standard allocator. What behaviour should be expected from the assigment operator? When would it be called? Why is it there at all, when you can't change allocators in containers? To give a little more depth, I have a class like this: class HeapAllocator { // All requirements of the allocator interface are met along with:
0
251
by: Mark A. Gibbs | last post by:
I'm having yet another headache with making a standard allocator. What behaviour should be expected from the assigment operator? When would it be called? Why is it there at all, when you can't change allocators in containers? To give a little more depth, I have a class like this: class HeapAllocator { // All requirements of the allocator interface are met along with:
0
9672
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
9519
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
10438
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
10001
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
9042
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
6780
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
5437
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...
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
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...

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.