2016-03-19 14 views
0

C++ 11'deki allocator_traits'i anlamıyorum, bu yüzden cpluspluswebsitesinden allocator_traits exmple'u denedim. Ancak bu kod, kendi web sitesinde olmasa bile, gcc 4.9, VS 2015'te derleyemez. Ayrıca, bu örnekte neden bir allocator_traits sözdizimini göremediğimi de anlamıyorum. Bu örneği yalnızca özel bir ayırıcı oluşturduğunu görebiliyorum, ardından vektördeki tamamlayıcıyı kullanabiliyorum. Allocator_traits hakkında bir şey yok.allocator_traits örneği, cplusplus web sitesinden

Bana yardım eden var mı?

// custom allocator example 
#include <cstddef> 
#include <iostream> 
#include <memory> 
#include <vector> 

template <class T> 
struct custom_allocator { 
    typedef T value_type; 
    custom_allocator() noexcept {} 
    template <class U> custom_allocator (const custom_allocator<U>&) noexcept {} 
    T* allocate (std::size_t n) { return static_cast<T*>(::new(n*sizeof(T))); } 
    void deallocate (T* p, std::size_t n) { ::delete(p); } 
}; 

template <class T, class U> 
constexpr bool operator== (const custom_allocator<T>&, const custom_allocator<U>&) noexcept 
{return true;} 

template <class T, class U> 
constexpr bool operator!= (const custom_allocator<T>&, const custom_allocator<U>&) noexcept 
{return false;} 

int main() { 
    std::vector<int,custom_allocator<int>> foo = {10,20,30}; 
    for (auto x: foo) std::cout << x << " "; 
    std::cout << '\n'; 
    return 0; 
} 

Cplusplus sitesinde derleme hatadır:

In member function 'T* custom_allocator<T>::allocate(std::size_t)': 
12:74: error: expected type-specifier before ')' token 
In instantiation of 'T* custom_allocator<T>::allocate(std::size_t) [with T = int; std::size_t = long unsigned int]': 
/usr/include/c++/4.9/bits/alloc_traits.h:357:32: required from 'static std::allocator_traits<_Alloc>::pointer std::allocator_traits<_Alloc>::allocate(_Alloc&, std::allocator_traits<_Alloc>::size_type) [with _Alloc = custom_allocator<int>; std::allocator_traits<_Alloc>::pointer = int*; std::allocator_traits<_Alloc>::size_type = long unsigned int]' 
/usr/include/c++/4.9/bits/stl_vector.h:170:46: required from 'std::_Vector_base<_Tp, _Alloc>::pointer std::_Vector_base<_Tp, _Alloc>::_M_allocate(std::size_t) [with _Tp = int; _Alloc = custom_allocator<int>; std::_Vector_base<_Tp, _Alloc>::pointer = int*; std::size_t = long unsigned int]' 
/usr/include/c++/4.9/bits/stl_vector.h:1287:27: required from 'void std::vector<_Tp, _Alloc>::_M_range_initialize(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = const int*; _Tp = int; _Alloc = custom_allocator<int>]' 
/usr/include/c++/4.9/bits/stl_vector.h:378:36: required from 'std::vector<_Tp, _Alloc>::vector(std::initializer_list<_Tp>, const allocator_type&) [with _Tp = int; _Alloc = custom_allocator<int>; std::vector<_Tp, _Alloc>::allocator_type = custom_allocator<int>]' 
25:57: required from here 
12:77: warning: no return statement in function returning non-void [-Wreturn-type] 

gcc 4.9 raporlarda benzer hatası.

VS 2015 raporlarını hata ayrıca hat ile 12 mesajı aşağıdaki gibidir:

Severity Code Description Project File Line Suppression State 
Error C2059 syntax error: ')' allocator_traits c:\users\jiang\documents\visual studio 2015\projects\allocator_traits\allocator_traits.cpp 12 
+0

cplusplus.com'un doğruluğu konusunda bir itibarı yoktur. – Brian

cevap

2

::operator new tarafından ::new değiştirin ve işe yarayacak. Muhtemelen internet sitesinde yazım hatası vardı. operator new numaralı telefonu aradığınızda, new ifadesini çağırmıyorsunuz.

+0

İkinci soruma cevap verebilir misin? Veya bu örneği biraz açıklayabilir misiniz? – laijiang

+0

Ayrıca kullanılan bir allocator özelliklerini göremiyorum. Bu örnek özel bir ayırıcı inşa ediyor ve başka bir şey değil. Özel ayırıcınız için ['std :: allocator_traits'] (http://en.cppreference.com/w/cpp/memory/allocator_traits) uzmanlaşmanız gerekir. ['Cppreference.com'] (http://en.cppreference.com/w/Main_Page) kullanmanızı öneririm, daha yüksek kalitede görünüyor. – vsoftco

+0

cppreference.com'un örneği yok. Benim için bir örnek verebilir misin? İnternette kullanılabilir bir allocator_traits örneği bulamıyorum. – laijiang

İlgili konular