If you specify an allocator in an STL container is it a requirement that
the allocator allocates object of the right type, or can you assume that
the container will rebind the allocator to the correct type?
For example I tried the following code which uses a 'wrong' allocator and
was slightly surrised to find it compiles on the three compilers I tried
it on
#include <vector>
#include <memory>
int main()
{
std::vector<int , std::allocator< double> > vec;
vec.push_back(1 );
}
john
Jul 22 '05
13 1952
Jonathan Turkanis wrote: "John Harrison" <jo************ *@hotmail.com> wrote in message news:opsav3hke1 212331@andronic us...
On Fri, 9 Jul 2004 15:21:25 -0600, Jonathan Turkanis <te******@kan garoologic.com> wrote:
"John Harrison" <jo************ *@hotmail.com> wrote in message news:opsav1x fa5212331@andro nicus... [...]I also noticed that Josuttis' book also ignores what the standard
says,
see first page of chapter 15 where he happily passes the same
allocator to
several containers with different value types.
It looks to me like the allocators he uses all have value_types appropriat e for the containers.
Well maybe I'm reading too much into this but in chpater 15 in quick succession he gives
vector<int,Sp ecialAlloc> v;
map<int,float ,less<int>,Spec ialAlloc> m;
basic_string< char,char_trait s<char>,Special Alloc> s;
That's funny. My copy has
vector<int, MyAlloc<int> > v; map<int,float,l ess<int>,MyAllo c<std::pair<con st int, float> > > m; ...
<Butting in...>
So does mine, but it's the 7th printing (what's yours?) so, either
before they printed something differently or after...
V
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:uv******** *********@ord-read.news.verio .net... Jonathan Turkanis wrote: "John Harrison" <jo************ *@hotmail.com> wrote in message news:opsav3hke1 212331@andronic us...
Well maybe I'm reading too much into this but in chpater 15 in
quicksuccession he gives
vector<int,Sp ecialAlloc> v;
map<int,float ,less<int>,Spec ialAlloc> m;
basic_string< char,char_trait s<char>,Special Alloc> s;
That's funny. My copy has
vector<int, MyAlloc<int> > v; map<int,float,l ess<int>,MyAllo c<std::pair<con st int, float> > >
m; ...
<Butting in...>
So does mine, but it's the 7th printing (what's yours?) so, either before they printed something differently or after...
Mine is the 10th. I wonder if Josuttis changed it because he decided
it was an error or because he thought it was debatable or an
unnecessary distraction.
I'm a bit suprised that the requirement that the value_type's match is
stated clearly for basic_string (21.3/1) but only in an almost
parenthetical way for containers generally (23.1/8), AFAICT.
Jonathan
>> Yes it seems so, both implementations of the STL I've checked do rebind the allocator for vector at least. This seems to directly contradict the requirements for allocator_type.
I think this approach gives meaning to an otherwise ill formed program, hence it's a conforming implementation.
Yes, thinking about it I agree.
john
On Fri, 9 Jul 2004 16:13:19 -0600, Jonathan Turkanis
<te******@kanga roologic.com> wrote: Mine is the 10th. I wonder if Josuttis changed it because he decided it was an error or because he thought it was debatable or an unnecessary distraction.
Mine's the third, the change is listed in the errata on his website.
john This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Bernhard Kick |
last post by:
Hi all,
I saw this code in the book "Accelerated C++" (chapt 11, iirc):
template <class T> class Vec {
...
std::allocator<T> alloc; // object to handle memory allocation
// ??would static member work here??:
// static std::allocator<T> alloc;
|
by: Orjan Westin |
last post by:
Hi,
I have an interesting (read frustrating) problem. I'm writing a
generic container class, which holds data as well as links to other
instances of itself, like this:
template<class T>
class node
{
public:
|
by: Alex Vinokur |
last post by:
I am looking for any custom allocator sample code for std::vector.
Thanks.
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn
|
by: Romeo Colacitti |
last post by:
I have a need to make a custom quasi-memory allocator, and I remembered
a simple ons in K&R2. Looking at the code for it now, I think I notice
a "fault" in the design, and I was wondering if people would back me up
on this.
The design basically uses a pool of memory, allocated as a character
array. Pointers into the array are retured by the allocated function.
Isn't this very dangerous, as a char has very lenient memory alignment...
|
by: Grahamo |
last post by:
Hi,
can anybody tell me where I can get the boiler plate code for
std::allocator. I need to have my version of new and delete called and
want to get reference code. My compilers headers are all over the place
with #ifdefs and what not, I'd like a clean copy.
thanks much
| |
by: xqxu.pzhou |
last post by:
I wrote a simple allocator "myAlloc" under the g++ 3.2.3. When it is
used by Vector, it works well. But when it is used by List, the codes
have errors when compling. the error message is:
"no matching function for call to myAlloc<std::_List_node<int>
Then I added the following copy constructor to "myAlloc", now it works
well.
"template <typename U>
myAlloc(const myAlloc<U>&) {}"
|
by: Chris Thomasson |
last post by:
This region allocator is not dynamic and can be fed with a buffer residing
on the stack of the calling thread. Therefore, you can use this in an
environment which does not have a heap; its basically analogous to `alloca'.
However, it does not align each buffer on a boundary sufficient for _any_
type. Instead, it dynamically calculates the types alignment requirements
and mutates the offset accordingly. This makes it more efficient wrt...
|
by: Juha Nieminen |
last post by:
I tested the speed of a simple program like this:
//------------------------------------------------------------
#include <list>
#include <boost/pool/pool_alloc.hpp>
int main()
{
typedef std::list<intList_t; // default allocator
//typedef std::list<int, boost::pool_allocator<int List_t;
|
by: PeterAPIIT |
last post by:
Hello all C++ expert programmer,
i have wrote partial general allocator for my container.
After reading standard C++ library and code guru article, i have
several questions.
1. Why allocator write forward declaration then allocation for void*
rather than directly wrote allocator first ?
|
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: 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...
|
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...
|
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: 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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |