簡體   English   中英

如何初始化一系列不可移動,不可復制的對象?

[英]How to initialize a sequence of non-movable, non-copyable objects?

假設我有一個既不可移動又不可復制的類型:

struct foo
{
  explicit foo( size_t ){}
  ~foo(){}

  foo( foo const & ) = delete;
  foo( foo && ) = delete;
  foo& operator=( foo const & ) = delete;
  foo& operator=( foo & ) = delete;
};

現在給定一個在編譯時已知的數字(稱為N),我有什么辦法可以在堆棧上創建這些的“序列”,並且每個序列都用數字0到N-1初始化? 我會對C樣式的數組foo[N]std::array< foo, N >甚至某種std::tuple感到滿意。

我要避免的是寫出:

foo f0( 0 ), f1( 1 ), ... fNminus1( N-1 );

當感覺像是編譯器應該能夠為我做的事情時。 我能想到的最好的方法是使用boost::optional

boost::optional< foo > f[N];

for( size_t i = 0U; i < N; ++i )
  f[i] = boost::in_place( i );

但這依賴於運行時邏輯,即使所有必需的信息在編譯時都可用。 另外,我還有一些行為類似於指針數組。

// create a type with the proper alignment
typedef std::aligned_storage<sizeof(foo), std::alignment_of<foo>::value>::type buffer_type;

const int N = 10;
// create an array of uninitialized raw data
buffer_type storage_buffer[N];

// initialize each foo object with placement new
for (size_t i=0; i<N; ++i)
    new (storage_buffer + i) foo(i);

foo * fp = (foo*)(&storage_buffer);
// access your foo objects via fp


// you must manually call the destructor of each object
for (size_t i=0; i<N; ++i)
    fp[i].~foo();

如果這看起來很麻煩,那就是。 但是您可以輕松地將該功能封裝在類中。

盡管不是嚴格意義上的數組,但您可以使用模板遞歸來完成此操作

template< typename T, size_t N >
struct type_array : public type_array< T, N-1 > {
    // this is the Nth element
    T elem;
    // it is constructed with N
    type_array() : elem( N ) {}

    // member function to return the Nth element
    T & get( size_t n ) {
        if ( n == N ) {
            return elem;
        } else {
            return type_array< T, N-1 >::get( n );
        }
    }
};

// base case when N == 0
template< typename T >
struct type_array<T, 0> {
    T elem;
    type_array() : elem( 0 ) {}
    T & get( size_t n ) {
      return elem;
    }
};

用法:

type_array< foo, 100 > foo_array;   // construct 100 foos
foo_array.get(1);                   // foo with n == 1
foo_array.get(2);                   // foo with n == 2

就像本傑明·林德利(Benjamin Lindley)的回答一樣,但都包含在課程中:

#include <type_traits>
#include <utility>
#include <new>

template<typename T>
class uninitialized {
public:
  constexpr uninitialized() { }

  ~uninitialized() {
    get().~T();
  }

  explicit uninitialized(const uninitialized& other) {
    construct(other);
  }

  explicit uninitialized(uninitialized&& other) {
    construct(std::move(other));
  }

  template<class... Args>
  explicit uninitialized(Args&&... args) { 
    construct(std::forward<Args>(args)...);
  }

  template<class... Args>
  void construct(Args&&... args) noexcept {
    static_assert(std::is_nothrow_constructible<T, Args...>::value, "constructor should not throw!");
    ::new(getPointer()) T (std::forward<Args>(args)...);
  }

  uninitialized& operator = (const T& t) { 
    get() = t;
    return *this;
  }

  uninitialized& operator = (T&& t) {
    get() = std::move(t);
    return *this;
  }

  T* operator -> () { return getPointer(); }    
  T& operator * () { return get(); }    
  T* operator & () { return getPointer(); }    
  T* getPointer() { return reinterpret_cast<T*>(&data); }    
  T& get() { return *reinterpret_cast<T*>(&data); }

  const T* operator -> () const { return getPointer(); }    
  const T& operator * () const { return get(); }    
  const T* operator & () const { return getPointer(); }    
  const T* getPointer() const { return reinterpret_cast<const T*>(&data); }    
  const T& get() const { return *reinterpret_cast<const T*>(&data); }

private:
  std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type data;
};

現在事情變得簡單一些:

uninitialized<foo> f[N];

for (size_t i = 0; i < N; ++i)
  f[i].construct(i);

for (const auto& fooref : f)
  fooref->bar();

// foo::~foo is called for you

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM