Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #71158
| From | Manfred <noname@add.invalid> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Wading through template instantiation errors for allocator |
| Date | 2020-01-05 22:31 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <qutkis$11sh$1@gioia.aioe.org> (permalink) |
| References | <2925c440-1188-48ff-b931-243234719891@googlegroups.com> <quo4tb$p2c$1@dont-email.me> <5b986e11-1d16-446c-9437-f29dfc8b6539@googlegroups.com> <h7bn71Fm8spU1@mid.individual.net> <a55bfda3-716a-4c8a-915a-b1ae1739fd23@googlegroups.com> |
On 1/4/2020 11:34 PM, Frederick Gotham wrote:
> Bo wrote:
>
>> rebind is only optional (provided by std::allocator_traits) if this
>> allocator is a template of the form SomeAllocator<T, Args>, where Args
>> is zero or more additional template type parameters."
>
>
> Well spotted. Here's my workaround:
>
>
> #include <cstddef> /* size_t */
> #include <new> /* Only for bad_alloc */
>
> template <std::size_t capacity>
> class Outer {
>
> template<typename T>
> class StaticAllocator {
> public:
> typedef T value_type;
>
> protected:
>
> static T buf[capacity];
>
> public:
>
> T *allocate(std::size_t const n)
> {
> if (n > capacity)
> throw std::bad_alloc();
>
> return buf;
> }
>
> void deallocate(T *, std::size_t)
> {
> /* Do Nothing */
> }
> };
>
> };
>
> template<std::size_t capacity>
> template<typename T>
> T Outer<capacity>::StaticAllocator<T>::buf[capacity];
>
> using std::size_t;
>
> #include <vector>
> using std::vector;
>
> #include <iostream>
> using std::cout;
> using std::endl;
>
> auto main(void) -> int
> {
> vector< char, Outer<4>::StaticAllocator<char> > 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, Outer<4>::StaticAllocator<char> > 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;
> }
>
>
> Now I can get back to testing my new allocator.
>
The following satisfies the rebind requirement, although I do not know
how this may play in a more elaborate context:
template<typename T, std::size_t capacity>
class StaticAllocator {
public:
typedef T value_type;
protected:
static T buf[capacity];
public:
template<typename U>
struct rebind
{
typedef StaticAllocator<U, capacity> other;
};
T *allocate(std::size_t const n)
{
if (n > capacity)
throw std::bad_alloc();
return buf;
}
void deallocate(T *, std::size_t)
{
/* Do Nothing */
}
};
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-03 07:04 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-03 16:57 +0000
Re: Wading through template instantiation errors for allocator Paavo Helde <myfirstname@osa.pri.ee> - 2020-01-03 21:32 +0200
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-04 06:12 -0800
Re: Wading through template instantiation errors for allocator Bo Persson <bo@bo-persson.se> - 2020-01-04 15:53 +0100
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-04 14:34 -0800
Re: Wading through template instantiation errors for allocator Manfred <noname@add.invalid> - 2020-01-05 22:31 +0100
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-06 02:12 -0800
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-06 23:52 -0800
Re: Wading through template instantiation errors for allocator Ian Collins <ian-news@hotmail.com> - 2020-01-07 21:15 +1300
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-07 01:01 -0800
Re: Wading through template instantiation errors for allocator Ian Collins <ian-news@hotmail.com> - 2020-01-08 07:31 +1300
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 19:50 +0100
Re: Wading through template instantiation errors for allocator Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-10 07:06 -0800
Re: Wading through template instantiation errors for allocator Frederick Gotham <cauldwell.thomas@gmail.com> - 2020-01-14 02:25 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:20 +0000
Re: Wading through template instantiation errors for allocator boltar@nowhere.org - 2020-01-08 10:12 +0000
Re: Wading through template instantiation errors for allocator Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-08 09:22 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-08 17:44 +0000
Re: Wading through template instantiation errors for allocator "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2020-01-08 23:00 +0100
Re: Wading through template instantiation errors for allocator Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-01-10 06:48 -0800
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:20 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:35 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 09:43 +0000
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:47 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:59 +0100
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:15 +0000
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 11:21 +0100
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:25 +0000
Re: Wading through template instantiation errors for allocator Melzzzzz <Melzzzzz@zzzzz.com> - 2020-01-07 10:02 +0000
Re: Wading through template instantiation errors for allocator Bo Persson <bo@bo-persson.se> - 2020-01-07 15:27 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:36 +0100
Re: Wading through template instantiation errors for allocator Bonita Montero <Bonita.Montero@gmail.com> - 2020-01-07 10:42 +0100
csiph-web