no class template named ‘rebind’ in ‘struct aligned_allocator<short int>

Hi I am new to vectors in cpp, when I compiled the below code I get this following error. What does this mean and what the code is doing ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

template <typename T>
struct aligned_allocator
{
  typedef T value_type;
	T* allocate(std::size_t num)
  {
    void* ptr = nullptr;
    if (posix_memalign(&ptr,4096,num*sizeof(T)))
      throw std::bad_alloc();
    return reinterpret_cast<T*>(ptr);
  }

  void deallocate(T* p, std::size_t num)
  {
    free(p);
  }
};


int main() {

	bool ignore_dc = true;
	const size_t blocks = 1024*1024*4;

	std::vector< int16_t, aligned_allocator<int16_t> > source_block(64*blocks);
}


error: no class template named ‘rebind’ in ‘struct aligned_allocator<short int>’
/proj/xbuilds/2017.4_0206_1/installs/lin64/Vivado/2017.4/lnx64/tools/gcc/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.6.3/../../../../include/c++/4.6.3/bits/stl_vector.h: In instantiation of ‘std::vector<short int, aligned_allocator<short int> >’:


error: no members matching ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka std::_Vector_base<short int, aligned_allocator<short int> >}::_M_allocate’ in ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka struct std::_Vector_base<short int, aligned_allocator<short int> >}’

error: no members matching ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka std::_Vector_base<short int, aligned_allocator<short int> >}::_M_deallocate’ in ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka struct std::_Vector_base<short int, aligned_allocator<short int> >}’

stl_vector.h:209:20: error: no members matching ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka std::_Vector_base<short int, aligned_allocator<short int> >}::_M_get_Tp_allocator’ in ‘std::vector<short int, aligned_allocator<short int> >::_Base {aka struct std::_Vector_base<short int, aligned_allocator<short int> >}’

stl_vector.h: In destructor ‘std::vector<_Tp, _Alloc>::~vector() [with _Tp = short int, _Alloc = aligned_allocator<short int>]’:
Last edited on
You did not provide enough types to fulfill the allocator interface. See:

http://www.cplusplus.com/reference/memory/allocator/
With C++11 or later: a user defined rebind is not required; in fact it is deprecated in C++17.

For a simple stateless allocator, like the aligned_allocator above, this would suffice:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
template < typename T > struct toy_allocator
{
    using value_type = T ;
    toy_allocator() noexcept = default ;
    template < typename U > toy_allocator( const toy_allocator<U>& ) noexcept {}
    template < typename U > toy_allocator( toy_allocator<U>&& ) noexcept {}

    /*[[nodiscard]]*/ T* allocate( std::size_t n )
    {
        // TO DO: replace this with your own custom implementation of allocate

        const auto nbytes = n * sizeof(T) ;
        std::cout << "toy_allocator<" << typeid(T).name()
                  << "> : allocate " << nbytes << " bytes\n" ;
        return static_cast<T*>( ::operator new(nbytes) ) ;
    }

    void deallocate( T* p, std::size_t n ) noexcept
    {
        // TO DO: replace this with your own custom implementation of deallocate

        const auto nbytes = n * sizeof(T) ;
        if( p && nbytes )
        {
            std::cout << "toy_allocator<" << typeid(T).name()
                      << "> : deallocate " << nbytes << " bytes\n" ;
            ::operator delete(p) ;
        }
    }
};

template< typename A, typename B >
bool operator== ( const toy_allocator<A>&, const toy_allocator<B>& ) noexcept
{ return true ; }

template< typename A, typename B >
bool operator!= ( const toy_allocator<A>&, const toy_allocator<B>& ) noexcept
{ return false ; }

http://coliru.stacked-crooked.com/a/9a7e81cb209dd3bc
http://rextester.com/DPTLH14379
Last edited on
Topic archived. No new replies allowed.