【发布时间】:2020-09-17 20:26:58
【问题描述】:
(小参考):
template<typename T>
struct IVector2 {
T x, y;
IVector2(T x, T y) :
x(x), y(y) { }
};
有没有办法做到这一点:
static const IVector2<int> floors; // lowest / highest floor
std::vector<std::array<IPerson, floors.x - floors.y>> requestQueue;
我需要将数组大小初始化为 floor.x 和 floor.y 之间的差异。 我一直在寻找有关如何初始化楼层的答案,我得到了这个
const IVector2<int> IElevatorHandler::floors(-1, 4);
但这并没有。
【问题讨论】:
-
std::array的大小应该在编译时可用,但在您的示例中,它不是。如果您需要具有运行时动态大小的容器,请使用std::vector而不是std::array。 -
我想你可以用
literal classes -
你只需要使用
constexpr:godbolt.org/z/HdqYvN
标签: c++ arrays initialization constants