我正在编写一个简单的类来在AVR微控制器上设置串行端口。有些参数只有几个有意义的值,例如波特率,奇偶校验类型或停止位数。因此,我想创建一个类型,整数的子集,可以是1或2。我可以创建一个枚举类型:
enum stopBits { one, two };
Run Code Online (Sandbox Code Playgroud)
我不喜欢这种解决方案(拼写出波特率值吗?)。我想出了这个:
template<int vv> struct stopBits {
static_assert( vv == 1 || vv == 2, "stop bit values can be 1 or 2");
int value = vv;
};
// usage:
stopBits<2> s;
Run Code Online (Sandbox Code Playgroud)
我更喜欢这种方式,并且喜欢从编译器输出中获得有用的错误消息。我宁愿能够使用复制构造函数来初始化s:
// I'd like to have this
stopBits s = 2;
Run Code Online (Sandbox Code Playgroud)
这样,我将能够编写一个类似以下内容的类:
serial::serial(baudRate b, stopBits s = 1, parity p = none);
Run Code Online (Sandbox Code Playgroud)
在寻找解决方案时,我发现自己陷入了困境:模板参数推导,bounded :: integer库,不能为constexpr的函数参数this和this。可以做到这一点,还是最好投降并继续前进?在此先感谢大家。
c++ ×1