3

I have following simple class with conversion:

#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; } 

When I try compile and run them I see following message:

[ 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); 

1 Answer 1

4

For static_cast,

Converts between types using a combination of implicit and user-defined conversions.

User-defined conversion will be considered, but you're casting to int on const object, you should make operator int() const member function:

operator int() const { ~~~~~ ... } 

For reinterpret_cast,

Converts between types by reinterpreting the underlying bit pattern.

Unlike static_cast, but like const_cast, the reinterpret_cast expression does not compile to any CPU instructions. It is purely a compiler directive which instructs the compiler to treat the sequence of bits (object representation) of expression as if it had the type new_type.

So reinterpret_cast<int>(x) won't work anyhow, and user-defined conversion won't be considered.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.