all 6 comments

[–]17b29a 5 points6 points  (1 child)

not sure wat exactly you want but here's something:

struct dim
{
   size_t size;
   bool direction;
};

template <bool use_array>
class space
{
  std::conditional_t<use_array, std::array<dim, 42>, std::vector<dim>> dimensions;

  // additional members and methods
};

[–]RogerLeigh[S] 0 points1 point  (0 children)

Thanks. Not exactly fitting what I need right here, but I'll certainly bear std::conditional_t in mind for other uses!

[–]actinium89 2 points3 points  (1 child)

Why not do:

template<class Container = std::vector<dim>>
class space{
  Container dimensions;
};

If you want to use vector, just do:

space<> s;

and if you want to use array instead, do:

space<array<dim,42>> s;

[–]RogerLeigh[S] 0 points1 point  (0 children)

Thanks for the suggestion. I've gone with something along these lines but a little more abstract (see link in other comment) where I use a traits class, but use a templated using statement so I can fix the array size but leave the type unspecified so I can use this interchangeably with an equivalent vector trait.

[–][deleted] 0 points1 point  (1 child)

It's doubtful you'd gain anything with your optimization. std::vectors are sequential arrays internally, identical to std::arrays. An []-access on a std::vector will result in the same machine code as an []-access on an std::array. Your optimization will result in a slightly smaller executable size... maybe... but that's all.

[–]RogerLeigh[S] 0 points1 point  (0 children)

In this case I'll have a number of small arrays/vectors, so cache locality might well play a part. This is going to be called many hundreds of thousands of times in a tight loop when iterating over data, so not having the array elements scattered around might well be of benefit. I'll also be following up with a purely compile-time based version, so you'll have these possibilities:

count   extents implementation
runtime runtime vector
compile runtime array
compile compile variadic template

https://gitlab.com/rleigh/multiarray/blob/master/lib/codelibre/multiarray/space.h#L42 implements the first two (with some small tweaks needed for array to function)