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


Groups > comp.lang.c++ > #71113

Re: Wading through template instantiation errors for allocator

From Bo Persson <bo@bo-persson.se>
Newsgroups comp.lang.c++
Subject Re: Wading through template instantiation errors for allocator
Date 2020-01-04 15:53 +0100
Message-ID <h7bn71Fm8spU1@mid.individual.net> (permalink)
References <2925c440-1188-48ff-b931-243234719891@googlegroups.com> <quo4tb$p2c$1@dont-email.me> <5b986e11-1d16-446c-9437-f29dfc8b6539@googlegroups.com>

Show all headers | View raw


On 2020-01-04 at 15:12, Frederick Gotham wrote:
> On Friday, January 3, 2020 at 7:33:08 PM UTC, Paavo Helde wrote:
> 
>> By deriving your allocator from std::allocator<T> it started to compile,
>> at least with my compiler and settings.
>>
>> Theoretically this derivation should not be needed, but then it would
>> probably need to define some extra stuff. The allocator requirements
>> have been in great flux in the recent standards, I'm not sure what it is
>> missing exactly.
> 
> 
> 
> I've tried this on three compilers: GNU, Microsoft, Clang
> 
> The original code which has two template parameters, "typename T, std::size_t capacity", only compiles on the Clang compiler.
> 
> The second version with only one template parameter, "typename T", compiles on all three compilers.
> 
> Since the second version works on all three compilers, I don't think that this problem is anything to do with how allocators are implemented in the respective standard libraries for these three compilers. Making the change from two parameters to one parameter shouldn't cause compilation to fail.
> 
> Here's the code for the second version which works on all three compilers (I've just commented out the 2nd parameter):
> 
> #include <cstddef>  /* size_t */
> #include <new>  /* Only for bad_alloc */
> 
> std::size_t constexpr capacity = 4;  /* This is instead of a template parameter */
> 
> template<typename T /*, std::size_t capacity*/ >
> 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<typename T /*, std::size_t capacity*/ >
> T StaticAllocator<T /*,capacity*/ >::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, StaticAllocator<char /*, 4*/ > > 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 */> > 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;
> }
> 

Seems like the culprit is the rebind member template from the allocator 
requirements. MSVC uses that to make sure that the allocator used for 
vector<T> really allocates T's:

using _Rebind_alloc_t =
  typename allocator_traits<_Alloc>::template rebind_alloc<_Value_type>;


The allocator table says:

A::template rebind<U>::other (optional)[1]

with the very important note:

"Notes:

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

https://en.cppreference.com/w/cpp/named_req/Allocator#cite_note-1


As your second template parameter is a non-type template parameter (the 
value 4), it doesn't *fully* comply with these requirements and so a 
compiler doesn't have to accept it.


Apparently, some compilers might work if the allocator is *almost* 
correct, but they don't have to.


     Bo Persson


Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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