Cast supernatural operations? if so, how?

Is it possible to override (C-style) in C ++?

Suppose I have code

double x = 42; int k = (int)x; 

Can I make the broadcast in the second line execute the code I wrote? Sort of

 // I don't know C++ // I have no idea if this has more syntax errors than words operator (int)(double) { std::cout << "casting from double to int" << std::endl; } 

The reason I ask is related to the question "Is there a way to get gcc or clang to warn about explicit translations?" and my offer is there.

+3
source share
4 answers

Β§ 12.3.1 / 1 "Type conversions of class objects can be specified by constructors and conversion functions. These conversions are called user conversions and are used for implicit type conversions (Section 4) for initialization (8.5), and for explicit type conversions (5.4, 5.2. 9).

Yes, we can do conversions, but only if one or both sides are a user-defined type, so we cannot do one for double to int .

 struct demostruct { demostruct(int x) :data(x) {} //used for conversions from int to demostruct operator int() {return data;} //used for conversions from demostruct to int int data; }; int main(int argc, char** argv) { demostruct ds = argc; //conversion from int to demostruct return ds; //conversion from demostruct to int } 

As Rob out pointed out, you can add the explicit keyword to any of these conversion functions, which requires the user to explicitly use them with (demostruct)argc or (int)ds , as in your code, instead of implicitly converting them. If you convert to the same type, it is usually better to have one or both as explicit , otherwise you may get compilation errors.

+4
source

Yes, but only for your own types. Look at this:

 #include <iostream> struct D { // "explicit" keyword requires C++11 explicit operator int() { std::cout << __FUNCTION__ << "\n"; } }; int main () { int i; D d; //i = d; i = (int)d; } 

So you cannot create double::operator int() , but you can create MyDouble::operator int() .

+3
source

You cannot overload operators for built-in types, but you can write a conversion operator for a custom type:

 struct Double { double value; operator int() const { shenanigans(); return value; } }; 

Since your question arose because of the need to find explicit casts in the code, also keep in mind that C ++ has explicit casting operators. They are not only clearer than C-style castes, but also searchable:

 static_cast<T>(x) // Cast based on static type conversion. dynamic_cast<T>(x) // Cast based on runtime type information. const_cast<T>(x) // Add or remove const or volatile qualification. reinterpret_cast<T>(x) // Cast between unrelated pointer and integral types. 
+2
source

Conversions with other types are overloaded operators in C ++ ( some examples here ), but this fact will not help you.

Stroustrup wanted the language to be extensible but not mutable. Therefore, operator overloading only expands operations to new types, but you cannot override what happens to any old types.

"However, to avoid absurdity, it is (as before) not allowed to provide new values ​​for built-in operators for built-in types. Thus, the language remains extensible, but not mutable."

+1
source

Source: https://habr.com/ru/post/914144/


All Articles