Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #71021 > unrolled thread

Wading through template instantiation errors for allocator

Started byFrederick Gotham <cauldwell.thomas@gmail.com>
First post2020-01-03 07:04 -0800
Last post2020-01-07 10:42 +0100
Articles 13 on this page of 33 — 11 participants

Back to article view | Back to comp.lang.c++


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#71256

FromTim Rentsch <tr.17687@z991.linuxsc.com>
Date2020-01-10 06:48 -0800
Message-ID<86d0br5pv2.fsf@linuxsc.com>
In reply to#71191
Ian Collins <ian-news@hotmail.com> writes:

> On 04/01/2020 04:04, Frederick Gotham wrote:
>
>> auto main(void) -> int
>
> What kind of an abomination is this?

As abominations go, it's one of the best.

[toc] | [prev] | [next] | [standalone]


#71193

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 09:20 +0000
Message-ID<83YQF.3544$GFe.3051@fx12.am4>
In reply to#71021
On 2020-01-03, Frederick Gotham <cauldwell.thomas@gmail.com> wrote:
>
> 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

[toc] | [prev] | [next] | [standalone]


#71195

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 09:35 +0000
Message-ID<ehYQF.7163$Arx.4170@fx08.am4>
In reply to#71193
On 2020-01-07, Melzzzzz <Melzzzzz@zzzzz.com> wrote:
> On 2020-01-03, Frederick Gotham <cauldwell.thomas@gmail.com> wrote:
>>
>> 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

[toc] | [prev] | [next] | [standalone]


#71198

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 09:43 +0000
Message-ID<GoYQF.2794$OEe.1962@fx15.am4>
In reply to#71195
On 2020-01-07, Melzzzzz <Melzzzzz@zzzzz.com> wrote:
> On 2020-01-07, Melzzzzz <Melzzzzz@zzzzz.com> wrote:
>> On 2020-01-03, Frederick Gotham <cauldwell.thomas@gmail.com> wrote:
>>>
>>> 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...
>
add this:
template <class U> struct rebind{ typedef StaticAllocator<U,capacity> other; };

-- 
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

[toc] | [prev] | [next] | [standalone]


#71199

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-07 10:47 +0100
Message-ID<qv1k2j$5lt$1@news.albasani.net>
In reply to#71198
>>>> 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...

> add this:
> template <class U> struct rebind{ typedef StaticAllocator<U,capacity> other; };

The rebind-member of an allocator is deprecated in C++17 and removed in
C++20. You would have to specialize std::allocator_traits<youralloc> for
your own allocator. I don't like this because having a own rebind-member
results in writing less code.

[toc] | [prev] | [next] | [standalone]


#71200

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-07 10:59 +0100
Message-ID<qv1kpk$4b5$1@news.albasani.net>
In reply to#71199
> The rebind-member of an allocator is deprecated in C++17 and removed in
> C++20. You would have to specialize std::allocator_traits<youralloc> for
> your own allocator. I don't like this because having a own rebind-member
> results in writing less code.

Hm, how do I specialize std::allocator_traits<Alloc> for a special
allocator? Can I do it this way:

template<>
template<typename T>
struct std::allocator_traits<virtual_allocator<T>>
{
};

With this I get a single error vom MSVC and myriads of errors from gcc.

[toc] | [prev] | [next] | [standalone]


#71202

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 10:15 +0000
Message-ID<uTYQF.1214$BA5.921@fx21.am4>
In reply to#71200
On 2020-01-07, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>> The rebind-member of an allocator is deprecated in C++17 and removed in
>> C++20. You would have to specialize std::allocator_traits<youralloc> for
>> your own allocator. I don't like this because having a own rebind-member
>> results in writing less code.
>
> Hm, how do I specialize std::allocator_traits<Alloc> for a special
> allocator? Can I do it this way:
>
> template<>
> template<typename T>
> struct std::allocator_traits<virtual_allocator<T>>
> {
> };
>
> With this I get a single error vom MSVC and myriads of errors from gcc.
Sorry, I haven't messed with templates long time, except in simple
cases.
I have to figure this out myself ;)

-- 
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

[toc] | [prev] | [next] | [standalone]


#71203

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-07 11:21 +0100
Message-ID<qv1m2r$1fn$1@news.albasani.net>
In reply to#71202
>> Hm, how do I specialize std::allocator_traits<Alloc> for a special
>> allocator? Can I do it this way:
>> template<>
>> template<typename T>
>> struct std::allocator_traits<virtual_allocator<T>>
>> {
>> };
>> With this I get a single error vom MSVC and myriads of errors from gcc.

> Sorry, I haven't messed with templates long time, except in simple
> cases.
> I have to figure this out myself ;)

Here's the reduced code that produces the same error with MSVC but
it compiles with g++:

template<typename T>
struct My
{
};

template<typename T>
struct Lib
{
};

template<>
template<typename T>
struct Lib<My<T>>
{
};

int main()
{
     Lib<My<int>> lmi;
}

[toc] | [prev] | [next] | [standalone]


#71204

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 10:25 +0000
Message-ID<80ZQF.2431$QS6.974@fx02.am4>
In reply to#71203
On 2020-01-07, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>> Hm, how do I specialize std::allocator_traits<Alloc> for a special
>>> allocator? Can I do it this way:
>>> template<>
>>> template<typename T>
>>> struct std::allocator_traits<virtual_allocator<T>>
>>> {
>>> };
>>> With this I get a single error vom MSVC and myriads of errors from gcc.
>
>> Sorry, I haven't messed with templates long time, except in simple
>> cases.
>> I have to figure this out myself ;)
>
> Here's the reduced code that produces the same error with MSVC but
> it compiles with g++:

Long time ago someone said: templates, write once, get compile errors
everywhere ;)

>
> template<typename T>
> struct My
> {
> };
>
> template<typename T>
> struct Lib
> {
> };
>
> template<>
> template<typename T>
> struct Lib<My<T>>
> {
> };
>
> int main()
> {
>      Lib<My<int>> lmi;
> }


-- 
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

[toc] | [prev] | [next] | [standalone]


#71201

FromMelzzzzz <Melzzzzz@zzzzz.com>
Date2020-01-07 10:02 +0000
Message-ID<UGYQF.2220$g%7.1182@fx07.am4>
In reply to#71199
On 2020-01-07, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>>>> 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...
>
>> add this:
>> template <class U> struct rebind{ typedef StaticAllocator<U,capacity> other; };
>
> The rebind-member of an allocator is deprecated in C++17 and removed in
> C++20. You would have to specialize std::allocator_traits<youralloc> for
> your own allocator. I don't like this because having a own rebind-member
> results in writing less code.

Hassle just to have additional parameter in allocator :(


-- 
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

[toc] | [prev] | [next] | [standalone]


#71206

FromBo Persson <bo@bo-persson.se>
Date2020-01-07 15:27 +0100
Message-ID<h7jiqfFbh1rU1@mid.individual.net>
In reply to#71199
On 2020-01-07 at 10:47, Bonita Montero wrote:
>>>>> 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...
> 
>> add this:
>> template <class U> struct rebind{ typedef StaticAllocator<U,capacity> 
>> other; };
> 
> The rebind-member of an allocator is deprecated in C++17 and removed in
> C++20. You would have to specialize std::allocator_traits<youralloc> for
> your own allocator. I don't like this because having a own rebind-member
> results in writing less code.

It is only removed from the std::allocator (where it is no longer needed 
now that we also have std::allocator_traits), but it is still an 
optional part of the allocator interface.


https://en.cppreference.com/w/cpp/named_req/Allocator



     Bo Persson

[toc] | [prev] | [next] | [standalone]


#71196

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-07 10:36 +0100
Message-ID<qv1jfp$74e$1@news.albasani.net>
In reply to#71021
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).

[toc] | [prev] | [next] | [standalone]


#71197

FromBonita Montero <Bonita.Montero@gmail.com>
Date2020-01-07 10:42 +0100
Message-ID<qv1jqa$593$1@news.albasani.net>
In reply to#71196
Am 07.01.2020 um 10:36 schrieb Bonita Montero:
> 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).

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>
#endif
#include <cstddef>
#include <exception>
#include <cassert>
#include <new>

#include <iostream>

template<class T>
class virtual_allocator
{
public:
     typedef T               value_type;
#if __cplusplus < 201703L
     typedef T              *pointer;
     typedef const T        *const_pointer;
     typedef T              &reference;
     typedef const T        &const_reference;
#endif
     typedef std::size_t     size_type;
     typedef std::ptrdiff_t  difference_type;
#if __cplusplus < 201703L
     template<typename U>
     struct rebind
     {
         typedef virtual_allocator<U> other;
     };
#endif

     explicit              virtual_allocator() = default;
     explicit              virtual_allocator( virtual_allocator const 
&other ) = default;
                           template<typename U>
                           virtual_allocator( virtual_allocator<U> const 
&other );
#if __cplusplus < 201703L
     static pointer        address( reference value );
     static const_pointer  address( const_reference value );
     static size_type      max_size() throw();
#endif
#if __cplusplus >= 201703L
     T                    *allocate( size_type n, void *hint = nullptr );
     void                  deallocate( T *p, size_type num = 1 );
#else
     pointer               allocate( size_type n, void *hint = nullptr );
     void                  deallocate( pointer p, size_type num = 1 );
#endif
#if __cplusplus < 201703L
     static void           construct( pointer p, const T&value );
     template<typename U>
     static void           destroy( U *p );
#endif
};

template<typename T>
template<typename U>
inline
virtual_allocator<T>::virtual_allocator( virtual_allocator<U> const 
&other ) :
     virtual_allocator()
{
}

#if __cplusplus < 201703L
template<typename T>
inline
typename virtual_allocator<T>::pointer virtual_allocator<T>::address( 
reference value )
{
     return &value;
}

template<typename T>
inline
typename virtual_allocator<T>::const_pointer 
virtual_allocator<T>::address( const_reference value )
{
     return &value;
}

template<typename T>
inline
typename virtual_allocator<T>::size_type 
virtual_allocator<T>::max_size() throw()
{
     return (size_t)(ptrdiff_t)-1 / sizeof(T);
}
#endif

template<typename T>
#if __cplusplus >= 201703L
T *virtual_allocator<T>::allocate( size_type n, void *hint )
#else
typename virtual_allocator<T>::pointer virtual_allocator<T>::allocate( 
size_type n, void *hint )
#endif
{
     std::cout << "allocate" << n << std::endl;
#if defined(_MSC_VER)
     T *p = (T *)VirtualAlloc( hint, n * sizeof(T), MEM_RESERVE | 
MEM_COMMIT, PAGE_READWRITE );
     if( !p )
         throw std::bad_alloc();
     return p;
#elif __unix__
     void *p = mmap( hint, n * sizeof(T), PROT_READ | PROT_WRITE, 
MAP_SHARED | MAP_ANONYMOUS, -1, 0 );
     if( p == MAP_FAILED )
         throw std::bad_alloc();
     return (pointer)p;
#else
     #error no implementation for your OS
#endif
}

template<typename T>
#if __cplusplus >= 201703L
inline
void virtual_allocator<T>::deallocate( T *p, size_type n )
#else
inline
void virtual_allocator<T>::deallocate( pointer p, size_type n )
#endif
{
     std::cout << "dellocate " << n << std::endl;
#if defined(_MSC_VER)
     BOOL ret = VirtualFree( p, 0, MEM_RELEASE );
     assert(ret);
#elif __unix__
     int ret = munmap( p, n * sizeof(T) );
     assert(ret == 0);
#else
     #error no implementation for your OS
#endif
}

#if __cplusplus < 201703L
template<typename T>
inline
void virtual_allocator<T>::construct( pointer p, const T&value )
{
     new( (void*)p ) T( value );
}

template<typename T>
template<typename U>
inline
void virtual_allocator<T>::destroy( U *p )
{
     p->~T();
}
#endif

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.c++


csiph-web