C++ C++;自定义范围为[0,64]的数据类型?

C++ C++;自定义范围为[0,64]的数据类型?,c++,struct,custom-data-type,C++,Struct,Custom Data Type,我寻找类似的问题,但没有找到答案 基本上,我需要声明/定义一个特定范围从0到64的变量。 我是否使用枚举?结构?我可以在声明变量时这样做吗? 我想在结构中使用它 当然,我可以编写一些if/else检查,但是必须有一种更简单的方法,不需要生成数组[64]。如果只需要一个受约束的整数,可以在构造函数中检查范围。像这样: #include <stdexcept> template<int max> class ConstrInt { public: ConstrInt(

我寻找类似的问题,但没有找到答案

基本上,我需要声明/定义一个特定范围从0到64的变量。 我是否使用枚举?结构?我可以在声明变量时这样做吗? 我想在结构中使用它


当然,我可以编写一些if/else检查,但是必须有一种更简单的方法,不需要生成数组[64]。

如果只需要一个受约束的整数,可以在构造函数中检查范围。像这样:

#include <stdexcept>

template<int max>
class ConstrInt {
public:
   ConstrInt(int val) {
      if (val > max)
         throw std::out_of_range("");
      m_val = val; 
   }

   int value() const {
      return m_val;
   }
private:
   int m_val;
};



int main(){
   ConstrInt<64> a{1};
   auto aa = a.value();
   ConstrInt<64> b{65};  // exception for > 64
}
#包括
模板
类构造{
公众:
ConstrInt(int-val){
如果(val>max)
抛出标准::超出范围(“”);
m_val=val;
}
int value()常量{
返回m_val;
}
私人:
国际货币基金组织;
};
int main(){
构造a{1};
自动aa=a.值();
ConstrInt b{65};//大于64的异常
}
我是否使用枚举

我不知道这有什么用

结构

如果要定义自定义数据类型,则需要定义类(结构是类)

我可以在声明变量时这样做吗

如果这是您的意思,您可以定义自定义数据类型的变量

当然,我可以编写一些if/else检查,但必须有一种更简单的方法,不必生成数组[64]

不需要仅仅为了检查某个值是否在某个范围内而创建数组。下面是一个示例检查:

if (x >= 0 && x <= 64)

if(x>=0&&x您可以定义一个带有
无符号字符的类,并使其像内置无符号整数类型一样进行换行,这样
0-1
将导致
64
,而
64+1
将导致
0

例如:

template<unsigned End = 64>
class usmall {
public:
    // implicit conversion constructor, making sure the value is in the range [0, End]
    usmall(unsigned char v = 0) : data(v % (End + 1)) {}

    // implicit user defined conversion operator
    operator unsigned char () const { return data; }

    // pre increment and decrement operators
    usmall& operator++() { if(data==End) data = 0; else ++data; return *this; }
    usmall& operator--() { if(data==0) data = End; else --data; return *this; }

    // post increment and decrement operators
    usmall operator++(int) { usmall rv(*this); ++*this; return rv; }
    usmall operator--(int) { usmall rv(*this); --*this; return rv; }

    // common arithmetic operators
    usmall& operator+=(const usmall& rhs) {
        data = (data + rhs.data) % (End + 1);
        return *this;
    }
    
    usmall& operator-=(const usmall& rhs) {
        if(data < rhs.data) data += (End + 1);
        data -= rhs.data;
        return *this;
    }

    usmall& operator*=(const usmall& rhs) {
        data = (data * rhs.data) % (End + 1);
        return *this;
    }
    
    usmall& operator/=(const usmall& rhs) {
        data /= rhs.data;
        return *this;
    }

private:
    unsigned char data;
};

// free arithmetic functions
template<unsigned End>
auto operator+(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv += rhs;
    return rv;
}

template<unsigned End>
auto operator-(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv -= rhs;
    return rv;
}

template<unsigned End>
auto operator*(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv *= rhs;
    return rv;
}

template<unsigned End>
auto operator/(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv /= rhs;
    return rv;
}
#include <iostream>

int main() {
    usmall foo = 3;
    usmall bar = 30;
    usmall baz = foo * bar;
    std::cout << static_cast<unsigned>(baz) << '\n'; // prints 25
}
模板
美国购物中心{
公众:
//隐式转换构造函数,确保值在[0,End]范围内
usmall(unsigned char v=0):数据(v%(End+1)){
//隐式用户定义转换运算符
运算符unsigned char()常量{return data;}
//预递增和递减运算符
usmall&operator++(){if(data==End)data=0;else++data;返回*this;}
usmall&operator-->{if(data==0)data=End;else--data;return*this;}
//后增量和减量运算符
usmall操作符++(int){usmall rv(*this);++*this;return rv;}
usmall运算符--(int){usmall rv(*this);-->this;return rv;}
//常用算术运算符
usmall和operator+=(const usmall和rhs){
数据=(数据+rhs.data)%(结束+1);
归还*这个;
}
usmall和运营商-=(const usmall和rhs){
如果(数据
例如:

template<unsigned End = 64>
class usmall {
public:
    // implicit conversion constructor, making sure the value is in the range [0, End]
    usmall(unsigned char v = 0) : data(v % (End + 1)) {}

    // implicit user defined conversion operator
    operator unsigned char () const { return data; }

    // pre increment and decrement operators
    usmall& operator++() { if(data==End) data = 0; else ++data; return *this; }
    usmall& operator--() { if(data==0) data = End; else --data; return *this; }

    // post increment and decrement operators
    usmall operator++(int) { usmall rv(*this); ++*this; return rv; }
    usmall operator--(int) { usmall rv(*this); --*this; return rv; }

    // common arithmetic operators
    usmall& operator+=(const usmall& rhs) {
        data = (data + rhs.data) % (End + 1);
        return *this;
    }
    
    usmall& operator-=(const usmall& rhs) {
        if(data < rhs.data) data += (End + 1);
        data -= rhs.data;
        return *this;
    }

    usmall& operator*=(const usmall& rhs) {
        data = (data * rhs.data) % (End + 1);
        return *this;
    }
    
    usmall& operator/=(const usmall& rhs) {
        data /= rhs.data;
        return *this;
    }

private:
    unsigned char data;
};

// free arithmetic functions
template<unsigned End>
auto operator+(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv += rhs;
    return rv;
}

template<unsigned End>
auto operator-(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv -= rhs;
    return rv;
}

template<unsigned End>
auto operator*(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv *= rhs;
    return rv;
}

template<unsigned End>
auto operator/(const usmall<End>& lhs, const usmall<End>& rhs) {
    usmall rv(lhs);
    rv /= rhs;
    return rv;
}
#include <iostream>

int main() {
    usmall foo = 3;
    usmall bar = 30;
    usmall baz = foo * bar;
    std::cout << static_cast<unsigned>(baz) << '\n'; // prints 25
}
#包括
int main(){
usmall-foo=3;
usmall bar=30;
usmall baz=foo*bar;

std::cout是否包含64?如果不包含,您可以使用一个位域。有固定的宽度。0和64都包含在内。如果有帮助,它应该表示Minecraft中的库存堆栈。最大堆栈大小为64。0为空。这取决于您想如何使用它以及为什么要使用它?例如,它是为了节省空间还是检测超出范围的错误?实际lly我希望我可以使用一个字符(较小),并将其限制为[0,64]。我不能定义一个范围为0到包括64的枚举,而不手动拼写每个值吗?@Kampfkeksgeschwader:enum的最小范围已经被四舍五入到略低于下一个二次方(即127)的值,您还将错过整数类型的算术运算。