重载类的' unsigned '说明符
Overload `unsigned` specifier for classes
我试图定义我自己的数据类型(称为sfloat
),它类似于浮点数,但使用不同数量的尾数位和指数位,以更好地适应我的数据范围和精度。我们的目标是定义一种新的数据类型,可以替换现有应用程序中的float。到目前为止,一切都解决了,只是我无法重写或定义unsigned
操作符,以便
unsigned sfloat(3.141527)
将返回这个类的无符号版本usfloat(3.141527)
。
似乎unsigned
说明符可能可以被重载,因为VS智能感知在头文件中没有抱怨:
sfloat::sfloat(float f) { m_data = get16bit(f); }
operator unsigned() { /*Do stuff here */ };
但是在声明和初始化中不起作用:
unsigned sfloat myPi= 3.141527; // Error: expected a ';'
我甚至不知道这在c++中是否可能做到,我很好奇以前是否有人这样做过?
由于c++默认为int表示签名,operator unsigned ()
只是operator unsigned int ()
的语法简写。用户定义的类型不能声明为signed
或unsigned
。
没有直接的方法可以完成你想要做的事情。正如@Angew在他的回答中提到的,unsigned
不能应用于用户定义的类型。
另一方面,您可以通过定义名为sfloat
和unsigned_sfloat
的类型来实现这一点,它们之间定义了转换。然后你可以写
unsigned_sfloat x(137.0f); // Close enough. ^_^
然后定义一个转换操作符
operator unsigned_sfloat() {
... implementation here ...
}
这给了你一些语法上接近你想要的东西,并解决了语言不允许你使用unsigned
关键字来修改自定义类型的事实。
希望这对你有帮助!
你可以用模板模拟如下:
#include <type_traits>
template <typename T = int>
class myfloat
{
static_assert(std::is_same<T, int>::value, "myfloat should only be instantiated on "signed" and "unsigned" ints");
const bool isSigned = true;
// the rest of the signed implementation
};
template <>
class myfloat<unsigned>
{
const bool isSigned = false;
// the rest of the unsigned implementation
};
int main()
{
myfloat<> a; // signed
myfloat<signed> b; // signed
myfloat<unsigned> c; // unsigned
// myfloat<float> d; // <-- compile error
return 0;
}
尝试如下:
template<typename T>
struct Unsigned;
,像这样使用
Unsigned<sfloat> usfloat
现在,您必须为类型Float
专门化Unsigned
,但这应该比unsigned_sfloat
类型更好地传达"是Float
的无符号版本"。但是,如果您想要将Unsigned<>
附加到这样的类型的整个库中,我才会这样做。
相关文章:
- 激励'inline'说明符的真实世界示例?
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 缺少类型说明符,显式类型为"缺少错误"
- 为什么mpfr_printf与十六进制浮点(%a转换说明符)的printf不同
- 叮叮当当在修复时插入多个"覆盖"说明符
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 编译器错误:destuctor 的更宽松的抛出说明符
- 使用说明符 extern 声明的C++中的标识符链接
- 同时具有"外部"和"内联"说明符的变量
- 与 tesseract::TessBaseApi() 相关的 Tesseract-OCR 出错(预期的类型说明符)
- 错误:不能使用"显式"说明符声明 bool'
- 错误:隐式转换更改符号:'int'到'unsigned long'
- 如何使用多次 memcpy 转换回 std::vector<unsigned char>?
- 基于范围的 for 循环range_declaration中各种说明符之间的性能差异
- 为什么转换函数声明不需要至少一个定义类型说明符
- NOEXCEPT 函数调用运算符的说明符_Not_fn
- 参数包内 noexcept 说明符
- C++需要所有声明的类型说明符和预期的非限定 id(需要调试帮助)
- 重载类的' unsigned '说明符