2012-09-20 3 views
5

this codeがコンパイルされないのはなぜですか?カスタムアロケータとデフォルトメンバー

#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; 
} 

ポインタ、参照、pointer_const & reference_constタイプを必要とするようです。しかし、cppreferenceによれば、これらのメンバーはすべてオプションです。 STLがallocator_traitを使用していなかったようです(私は-std = C++ 11でコンパイルしていますので、それが良いはずです)。

[編集]打ち鳴らすには、エラーは以下のとおりです。

[email protected]/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. 
+0

エラーは何ですか? – Nick

+0

@ニック私の質問をclang出力で更新しました。(gccも失敗しています) –

+0

基本的にすべてのコンパイラのためのC++ 11のサポートはまだ実験的/不完全です。したがって、単に実装のバグかもしれません(標準の関連する部分を参照することはできませんので、そのメンバーが実際にはオプションであるかどうかは分かりません) – Grizzly

答えて

2

これはGCCのC++標準ライブラリのバグです。

リストを使用している場合、allocator_traitsを使用してアロケータへのアクセスを適切にラップしていません。

ただし、ベクトルを正しく実装しています。 std::listの代わりにstd::vectorを使用した場合、このコードはコンパイルされます。

関連する問題