我可以初始化const实例,以便将其用作const来初始化数组吗? [重复]

问题描述 投票:0回答:1

(小参考):

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;

我需要初始化数组大小以使floors.x和floors.y之间存在差异。我一直在寻找有关如何初始化楼层的答案,而我已经知道了]

const IVector2<int> IElevatorHandler::floors(-1, 4);

但是那不行。

(小参考):template struct IVector2 {T x,y; IVector2(T x,T y):x(x),y​​(y){}};有没有办法做到这一点:静态const IVector2 ... ...>

c++ arrays initialization const
1个回答
1
投票

您可以尝试使用文字类。我对您的代码进行了一些更改

template<typename T>
struct IVector2 {
    T x, y;

    constexpr IVector2(T x, T y) : //constexpr constructor
        x(x), y(y) { }
};


static constexpr IVector2<int> floors(10,1);  // Initialized constexpr object. Now available at compile time
© www.soinside.com 2019 - 2024. All rights reserved.