How can the allowed range of an integer be restricted with compile time errors?

后端 未结 5 1726
失恋的感觉
失恋的感觉 2020-12-09 22:51

I would like to create a type that is an integer value, but with a restricted range. Attempting to create an instance of this type with a value outside the allowable range s

5条回答
  •  轮回少年
    2020-12-09 22:56

    Well, as you noticed, there is already a form of diagnostic for enumerations.

    It's generally crude: ie the checking is "loose", but could provide a crude form of check as well.

    enum Range { Min = 0, Max = 31 };
    

    You can generally assign (without complaint) any values between the minimal and maximal values defined.

    You can in fact often assign a bit more (I think gcc works with powers of 2).

提交回复
热议问题