I'm new to optimization in C++. I have read that stack allocated memory can be much faster than heap allocated memory.
I also have read that std::array is stack allocated, but most other containers, like std::vector or dynamic arrays are heap allocated. I'd like to define a class which essentially just stores an array of doubles. I intend for all instances of the class to have the same dimension, and that I can calculate what that dimension will be at compile time. The catch is that I would like to do the computation of that dimension in main.cpp instead of in the class.cpp. That means trying something like the following:
class.h:
extern constexpr dimension;
and,
class.cpp:
class Coordinates {
public std::array<double, dimension> q{};
}
and then
main.cpp:
#include "class.h"
constexpr dimension = 2*3*100
Now, extern constexpr dimension
is nonsense as the translation unit will not know what the value of dimension is at compile time.
Is there a way to have a stack-allocated array type object with dimension defined in another translation unit? Would it even be worth it?