Tuesday, January 7, 2020

Digest for comp.lang.c++@googlegroups.com - 25 updates in 5 topics

Frederick Gotham <cauldwell.thomas@gmail.com>: Jan 06 11:52PM -0800

I have cleaned it up again a little. This time I'm using a default template parameter.
 
#include <cstddef> /* size_t */
#include <new> /* Only for bad_alloc */
 
static int this_translation_unit; /* This serves the same purpose as __FILE__ */
 
template<typename T, std::size_t t_capacity, std::size_t t_counter, int *t_file = &this_translation_unit>
class StaticAllocator {
protected:
 
static T buf[t_capacity];
 
public:
 
typedef T value_type;

template<typename U>
struct rebind {
typedef StaticAllocator<U, t_capacity, t_counter, t_file> other;
};
 
T *allocate(std::size_t const n)
{
if (n > t_capacity)
throw std::bad_alloc();
 
return buf;
}
 
void deallocate(T *, std::size_t)
{
/* Do Nothing */
}
};
 
template<typename T, std::size_t t_capacity, std::size_t t_counter, int *t_file>
T StaticAllocator<T, t_capacity, t_counter, t_file>::buf[t_capacity];
 
using std::size_t;
 
#include <vector>
using std::vector;
 
#include <iostream>
using std::cout;
using std::endl;
 
auto main(void) -> int
{
vector< char, StaticAllocator< char, 4, __COUNTER__ > > v;
 
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
 
for (auto const &elem : v)
cout << elem << endl;

vector< char, StaticAllocator< char, 4, __COUNTER__ > > v2;
 
v2.push_back('x');
v2.push_back('y');
v2.push_back('z');
 
for (auto const &elem : v2)
cout << elem << endl;

// Now try the first vector again

for (auto const &elem : v)
cout << elem << endl;
}
 
I'm submitting this to the Boost development mailing list today.
Ian Collins <ian-news@hotmail.com>: Jan 07 09:15PM +1300

On 04/01/2020 04:04, Frederick Gotham wrote:
 
 
> auto main(void) -> int
 
What kind of an abomination is this?
 
--
Ian.
Frederick Gotham <cauldwell.thomas@gmail.com>: Jan 07 01:01AM -0800

On Tuesday, January 7, 2020 at 8:15:43 AM UTC, Ian Collins wrote:
 
> > auto main(void) -> int
 
> What kind of an abomination is this?
 
 
It fails to compile on older C++ compilers, before C++11.
Melzzzzz <Melzzzzz@zzzzz.com>: Jan 07 09:20AM


> My GNU C++ compiler version 7.4.0 for Ubuntu won't compile the following code and I don't know why.
 
Because allocator has only one template parameter and that's expected.
As I see it.
 
 
--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala
Melzzzzz <Melzzzzz@zzzzz.com>: Jan 07 09:20AM

> On 04/01/2020 04:04, Frederick Gotham wrote:
 
>> auto main(void) -> int
 
> What kind of an abomination is this?
 
People write some other language that is not C++ ;)
 
--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala
Melzzzzz <Melzzzzz@zzzzz.com>: Jan 07 09:35AM


>> My GNU C++ compiler version 7.4.0 for Ubuntu won't compile the following code and I don't know why.
 
> Because allocator has only one template parameter and that's expected.
> As I see it.
 
And you need rebind if allocator has additional arguments...
 
--
press any key to continue or any other to quit...
U ničemu ja ne uživam kao u svom statusu INVALIDA -- Zli Zec
Svi smo svedoci - oko 3 godine intenzivne propagande je dovoljno da jedan narod poludi -- Zli Zec
Na divljem zapadu i nije bilo tako puno nasilja, upravo zato jer su svi
bili naoruzani. -- Mladen Gogala
Bonita Montero <Bonita.Montero@gmail.com>: Jan 07 10:36AM +0100

Forget writing an allocator that doesn't have full heap-semantics.
The C++-containers often rebind the allocator-type for orther
allocators; f.e. an unordered_map might do a rebind for the
bucket-nodes (it gets only a allocator<pair<const Key, T>,
which isn't sufficient for everything a unordered_map needs).
Bonita Montero <Bonita.Montero@gmail.com>: Jan 07 10:42AM +0100

Am 07.01.2020 um 10:36 schrieb Bonita Montero:
> allocators; f.e. an unordered_map might do a rebind for the
> bucket-nodes (it gets only a allocator<pair<const Key, T>,
> which isn't sufficient for everything a unordered_map needs).
 
I tried to write my own allocator that relies on VirtualAlloc() or
mmap(). But it doesn't work for the above reason; i.e. the allocator
might get rebound and the new allocator might be used for tiny amounts
of memory, rounded up to a page-size.
 
#ifdef _MSC_VER
#include <Windows.h>
#elif __unix__
#include <sys/mman.h>

No comments: