5

Let's say I have a type which is neither movable nor copyable:

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

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

Now given a number known at compile time (call it N), is there any way that I can create a "sequence" of these on the stack with each one initialized with numbers 0 through N-1? I would be satisfied with a C-style array foo[N], a std::array< foo, N >, or perhaps even a std::tuple of some kind.

What I'm trying to avoid is writing out:

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

when it feels like this is something the compiler should be able to do for me. The best I've been able to come up with is using boost::optional.

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

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

But that relies on runtime logic even though all the required information is available at compile-time. Plus, I'm left with something that behaves like an array of pointers.

Andrew Durward
  • 3,771
  • 1
  • 19
  • 30

3 Answers3

3
// 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();

If that seems like a lot of hassle, it is. But you could easily encapsulate that functionality in a class.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
1

Although not strictly an array, you can sort of accomplish this with template recursion

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

Usage:

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
yngccc
  • 5,594
  • 2
  • 23
  • 33
1

Like the answer from Benjamin Lindley, but packed in a class:

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

Now things are a little bit easier:

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
R1tschY
  • 3,032
  • 1
  • 24
  • 30
  • I am afraid I have to vote this down. The problem here is that if `construct` throws an exception, you will call the destructor of an object that has never been constructed. – David Stone Dec 05 '15 at 18:50
  • @DavidStone Yes this is a problem. The constructor should not throw. I added a `static_assert`. In the answer from Benjamin Lindley, when the constructor fails, no destructor is called. – R1tschY Dec 06 '15 at 10:24