简体   繁体   English

自定义分配器和默认成员

[英]Custom allocator & default member

Why isn't this code compiling ? 为什么这段代码没有编译?

#include <cstdlib>
#include <list>

template < typename Type >
class Allocator {
public:
    using value_type = Type;
public:
    template < typename Other >
    struct rebind { using other = Allocator< Other >; };
public:
    Type * allocate( std::size_t n ) { return std::malloc( n ); }
    void deallocate( Type * p, std::size_t ) throw ( ) { std::free( p ); }
};

int main( void ) {
    std::list< void *, Allocator< void * > > list;
    return 0;
}

It seems to need pointer, reference, pointer_const & reference_const types. 它似乎需要指针,引用,pointer_const和reference_const类型。 However, according to cppreference these members are all optionals. 但是,根据cppreference,这些成员都是可选项。 It seems like if the STL weren't using allocator_trait (I'm compiling with -std=c++11 so it should be good). 似乎STL没有使用allocator_trait(我正在使用-std = c ++ 11进行编译,所以它应该是好的)。

Any idea ? 任何的想法 ?

[edit] On clang, errors are : [编辑]在clang上,错误是:

user@/tmp > clang++ -std=c++11 test.cc
In file included from test.cc:2:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/list:63:
/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_list.h:449:40: error: no type named 'pointer' in 'Allocator<void *>'
      typedef typename _Tp_alloc_type::pointer           pointer;
              ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
test.cc:17:46: note: in instantiation of template class 'std::list<void *, Allocator<void *> >' requested here
    std::list< void *, Allocator< void * > > list;
                                             ^
In file included from test.cc:2:
In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/list:63:
/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_list.h:450:40: error: no type named 'const_pointer' in 'Allocator<void *>'
      typedef typename _Tp_alloc_type::const_pointer     const_pointer;
              ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_list.h:451:40: error: no type named 'reference' in 'Allocator<void *>'
      typedef typename _Tp_alloc_type::reference         reference;
              ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
/usr/lib/gcc/i686-pc-linux-gnu/4.7.1/../../../../include/c++/4.7.1/bits/stl_list.h:452:40: error: no type named 'const_reference' in 'Allocator<void *>'
      typedef typename _Tp_alloc_type::const_reference   const_reference;
              ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
4 errors generated.

This is a bug in GCC's C++ standard library. 这是GCC的C ++标准库中的一个错误。

When using a list, they are not properly wrapping access to the allocator through an allocator_traits. 使用列表时,它们没有通过allocator_traits正确地包装对分配器的访问。

However, they do implement vector correctly. 但是,它们确实正确地实现了向量。 This code would compile if you used std::vector instead of std::list . 如果您使用std::vector而不是std::list则会编译此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM