473,804 Members | 3,057 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 2228
"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
9706
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
10583
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
10337
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...
1
10323
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,...
0
6854
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
5525
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...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2995
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.