【问题标题】:How to initialize a sequence of non-movable, non-copyable objects?如何初始化一系列不可移动、不可复制的对象?
【发布时间】:2013-04-12 03:21:39
【问题描述】:

假设我有一个既不能移动也不能复制的类型:

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 );

但这依赖于运行时逻辑,即使所有必需的信息在编译时都可用。另外,我还剩下一些行为类似于指针数组的东西。

【问题讨论】:

    标签: c++ constructor compile-time


    【解决方案1】:
    // 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();
    

    如果这看起来很麻烦,那就是。但是您可以轻松地将该功能封装在一个类中。

    【讨论】:

      【解决方案2】:

      虽然不是严格意义上的数组,但您可以通过模板递归来完成此操作

      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
      

      【讨论】:

        【解决方案3】:

        就像本杰明·林德利的回答,但集中在课堂上:

        #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
        

        【讨论】:

        • 恐怕我不得不投反对票。这里的问题是,如果construct抛出异常,你会调用一个从未构造过的对象的析构函数。
        • @DavidStone 是的,这是个问题。构造函数不应该抛出。我添加了static_assert。在 Benjamin Lindley 的回答中,当构造函数失败时,不会调用析构函数。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-05-15
        • 2019-08-23
        • 1970-01-01
        • 2019-08-25
        • 2011-10-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多