2012-09-20 27 views
5

Neden this code derleniyor?Özelleştirici ve varsayılan üye

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

işaretçi, başvuru, pointer_const & reference_const türleri gerek gibi görünüyor. Ancak, cppreference'a göre, bu üyelerin hepsi isteğe bağlıdır. STL allocator_trait kullanmıyormuş gibi görünüyor (-std = C++ 11 ile derlediğim için iyi olmalı).

Herhangi bir fikrin var mı? clang Açık

[değiştir], hataları şunlardır:

[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

Hata nedir? – Nick

+0

@Nick Sorgumu clang çıkışıyla güncelledim (gcc de başarısız oluyor) –

+0

Temel olarak tüm derleyiciler için C++ 11 desteğinin hala deneysel/eksik olduğunu unutmayın. Bu nedenle, basit bir uygulama hatası olabilir (şu anda standardın ilgili bölümlerini araştıramazsınız, bu yüzden bu üyelerin gerçekten isteğe bağlı olup olmadığını tahmin edin) – Grizzly

cevap

2

Bu GCC en C++ standart kütüphanesinde bir hata değildir.

Bir liste kullanıldığında, allocator_traits aracılığıyla allocator'a erişimi düzgün bir şekilde sarmalamazlar. Bununla birlikte, doğru bir şekilde vektör uygularlar. Bu kod, std::list yerine std::vector kullandıysanız derlenir.