2016-03-19 3 views
0

Ich verstehe nicht allocator_traits in C++ 11, so versuchte ich die allocator_traits exmple von cpluspluswebsite. Dieser Code kann jedoch nicht in gcc 4.9, VS 2015 kompiliert werden, auch nicht in seiner eigenen Website.allocator_traits Beispiel von cplusplus Website

Was mehr ist, verstehe ich auch nicht, warum ich in diesem Beispiel keine allocator_traits-Syntax sehen kann. Ich kann nur sehen, dieses Beispiel einen benutzerdefinierten Zuordner erstellen, dann den Zuordner in Vektor verwenden. Nichts über allocator_traits.

Kann mir jemand helfen?

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

Der Übersetzungsfehler in cplusplus Website ist:

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 Berichte ähnliche Fehler.

VS 2015 meldet Fehler auch in Zeile 12. Die folgende Nachricht ist:

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 hat keinen Ruf für Genauigkeit. – Brian

Antwort

2

ersetzen ::new durch ::operator new und es wird funktionieren. Wahrscheinlich war es ein Tippfehler auf der Website. Sie rufen hier operator new an und rufen keinen new Ausdruck auf.

+0

Können Sie meine zweite Frage beantworten? Oder kannst du dieses Beispiel ein wenig erklären? – laijiang

+0

Ich sehe auch keine Zuweisungsmerkmale. In diesem Beispiel scheint ein benutzerdefinierter Allokator zu entstehen und nichts mehr. Sie müssten ['std :: allocator_traits '] (http://en.cppreference.com/w/cpp/memory/allocator_traits) für Ihren benutzerdefinierten Zuordner spezialisieren. Ich würde empfehlen, dass Sie ['cppreference.com'] (http://en.cppreference.com/w/Main_Page) verwenden, es scheint von höherer Qualität zu sein. – vsoftco

+0

cppreference.com hat kein Beispiel. Kannst du mir ein Beispiel geben? Ich kann kein verfügbares allocator_traits-Beispiel im Internet finden. – laijiang