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.