2017-02-05 6 views
2

벡터에 대한 사용자 지정 할당자를 작성하려고합니다.clang 용 사용자 지정 할당 자의 추가 이동 구성

GCC 6.3에
#include <iostream> 
#include <vector> 

struct Noisy 
{ 
    Noisy() { std::cout << "Default ctor called" << '\n'; } 
    Noisy(const Noisy&) { std::cout << "Copy ctor called" << '\n'; } 
    Noisy& operator=(const Noisy&) { std::cout << "Copy assignment called" << '\n'; return *this; } 

    Noisy(Noisy&&) { std::cout << "Move ctor called" << '\n'; } 
    Noisy& operator=(Noisy&&) { std::cout << "Move assignment called" << '\n'; return *this; } 
}; 

template <typename T> 
struct StackAllocator : Noisy 
{ 
    using value_type = T; 
    T* allocate(std::size_t n) { return nullptr; } 
    void deallocate(T* p, std::size_t n) { } 
}; 

int main() 
{ 
    using alloc_t = StackAllocator<int>; 
    auto alloc = alloc_t{}; 
    std::vector<int, alloc_t> vec(alloc); 
} 

이 내가 기대했던 생산 : (Online link)

Default ctor called 
Copy ctor called 

그러나, 그 소리 3.9에,이 생산 : (Online link)

이 지금까지 스켈 톤 코드
Default ctor called 
Copy ctor called 
Move ctor called 
Move ctor called 

이것은 clang 벡터 구현의 소스 코드입니다. (here), 두 가지 추가 이동 구성을 설명 할 수있는 항목을 찾을 수 없습니다. (흥미롭게도 압축 된 쌍을 사용하여 빈 할당자가 EBO를 이용할 수 있음을 발견했습니다)

답변

1

tldr; __second_ 회원


  • 이동합니다 __compressed_pair 생성자에

    1. 복사 __libcpp_compressed_pair_imp 생성자에
    2. 이동 : 방금 통화, 복사 및 두 개의 움직임을 따르는 경우에서 온

      전화 : vector(allocator_type const&) :

      호출

      __vector_base(allocator_type const&) : __end_cap_ 우리가 생성자 __compressed_pair(pointer, allocator_type) 호출 __compressed_pair<pointer, allocator_type>입니다

      template <class _Tp, class _Allocator> 
      inline _LIBCPP_INLINE_VISIBILITY 
      __vector_base<_Tp, _Allocator>::__vector_base(const allocator_type& __a) 
          : __begin_(nullptr), 
           __end_(nullptr), 
           __end_cap_(nullptr, __a) 
      { 
      } 
      

      : 이제

      _LIBCPP_INLINE_VISIBILITY __compressed_pair(_T1_param __t1, _T2_param __t2) 
          : base(_VSTD::forward<_T1_param>(__t1), _VSTD::forward<_T2_param>(__t2)) {} 
      

      을, 우리의 종류도는 동일하며 빈 또는 최종 없다 둘 , 우리가 호출하는 기본 생성자는 __libcpp_compressed_pair_imp<pointer, allocator_type, 0>(pointer, allocator_type) :

      _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp(_T1_param __t1, _T2_param __t2) 
          : __first_(_VSTD::forward<_T1_param>(__t1)), __second_(_VSTD::forward<_T2_param>(__t2)) {} 
      
      입니다.
  • +0

    똥! 나는 압축 된 쌍에서 멈춰 섰다. 그 시점에서 나는 http://www.boost.org/doc/libs/1_63_0/libs/utility/doc/html/compressed_pair.html을보기 시작했다. 감사! 그것은 두 가지 추가 이동 구조를 설명합니다. 이 코멘트를 설명 할 수 있겠습니까? "이제는 우리 유형 모두 동일하지 않으며 둘 다 비어 있거나 최종 적이 지 않습니다. 그래서 우리가 호출하는 기본 생성자는" – skgbanga

    +1

    @skgbanga입니다. "compressed_pair"의 "compressed"은 조금 더 많다는 것을 나타냅니다 바로 두 멤버보다 복잡합니까? 이 경우에는 압축 할 수있는 것이 없습니다. – Barry