static_cast和interpret_cast的转换运算符int()失败
Conversion operator int() failed for static_cast and reinterpret_cast
我有以下带有转换的简单类:
#include <iostream>
using namespace std;
template<int p>
class Mod {
int n;
friend Mod operator+(const Mod &x, const Mod &y) {
const int sum = x.n + y.n;
if (sum < p) {
Mod z(sum);
return z;
} else if (sum >= p) {
Mod z2(sum - p);
return z2;
}
}
operator int() {
if (p == 0) return n;
if (p == 1) return 0;
for (int r = 0; r <= abs(n); r++) {
if (((r - n) % p) == 0) {
return r;
}
}
}
friend Mod operator-(const Mod &x) {
if (x.n == 0) return 0;
Mod z(-static_cast<int>(x) + p);
return z;
}
friend std::ostream &operator<<(std::ostream &out, const Mod &x) {
out << reinterpret_cast<int>(x); // invalid cast from type ‘const Mod<5>’ to type ‘int’ out << reinterpret_cast<int>(x);
return out;
}
public:
Mod(int x) {
n = x;
}
};
int main() {
Mod<5> z(3), x(2);
cout << z + x << endl; // error: invalid cast from type ‘const Mod<5>’ to type ‘int’ out << reinterpret_cast<int>(x);
cout << z << endl; //error: invalid cast from type ‘const Mod<5>’ to type ‘int’ out << reinterpret_cast<int>(x); // invalid cast from type ‘const Mod<5>’ to type ‘int’ out << reinterpret_cast<int>(x);
cout << -x << endl; //error: invalid static_cast from type ‘const Mod<5>’ to type ‘int’ Mod z(-static_cast<int>(x) + p);
return 0;
}
当我尝试编译并运行它们时,我看到以下消息:
[ 33%] Building CXX object CMakeFiles/TestCpp.dir/main.cpp.o
/home/user/ClionProjects/TestCpp/main.cpp: In instantiation of ‘std::ostream& operator<<(std::ostream&, const Mod<5>&)’:
/home/user/ClionProjects/TestCpp/main.cpp:75:17: required from here
/home/user/ClionProjects/TestCpp/main.cpp:56:13: error: invalid cast from type ‘const Mod<5>’ to type ‘int’
out << reinterpret_cast<int>(x); // invalid cast from type ‘const Mod<5>’ to type ‘int’ out << reinterpret_cast<int>(x);
^
/home/user/ClionProjects/TestCpp/main.cpp: In instantiation of ‘Mod<5> operator-(const Mod<5>&)’:
/home/user/ClionProjects/TestCpp/main.cpp:77:14: required from here
/home/user/ClionProjects/TestCpp/main.cpp:50:36: error: invalid static_cast from type ‘const Mod<5>’ to type ‘int’
Mod z(-static_cast<int>(x) + p);
对于static_cast,
使用隐式转换和用户定义转换的组合在类型之间进行转换。
将考虑用户定义的转换,但您在const
对象上转换为int
,您应该使operator int()
常量成员函数:
operator int() const {
~~~~~
...
}
对于interpret_cast,
通过重新解释基础位模式在类型之间进行转换。
与
static_cast
不同,但与const_cast
一样,reinterpret_cast
表达式不编译为任何CPU指令。它纯粹是一个编译器指令,指示编译器将表达式的位序列(对象表示)视为类型new_type。
所以reinterpret_cast<int>(x)
无论如何都不会工作,也不会考虑用户定义的转换。
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 如何理解C++标准N3337中的expr.const.cast子句8
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 有符号的int和int-有没有一种方法可以在C++中区分它们
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 是否可以从int转换为enum类类型
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 'short int'持有的值溢出,但"自动"不会溢出?
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 调用'begin(int [n])'没有匹配函数
- 没有显式声明的int[]中的foreach
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- 是否可以使用gcc 3.3版修复与int*cast相关的Sun Solaris OS 5.8分段故障
- int cast 和 int& cast 之间的区别
- "invalid cast from type 'const myClass' to type 'int'",如何使其有效?
- 编译程序时出错C++:"error: cast from ‘double*’ to ‘int’ loses precision"
- Cast int to void* and void* to int for TMemoryStream->WriteBuffer (C++Builder XE7)