2
votes

I have been trying to wrap my head around the differences between type casting and type conversion. What i've learnt is that essentially when the underlying value or bit representation of one type is changed to another value, we have performed a conversion. While in pure type casting we simply tell the compiler to treat the bit-pattern as another type. I used 'pure' because a casting may lead to a conversion in which case it is called an 'explicit conversion'.

I have thought of two examples to illustrate this difference. First:

float x = 1.5;
float* p1 = &x;
int* p2 = (int*) p1;
int i = *p2;

Here casting a pointer type performs no operation on the pointer value. So it's pure casting. In this case it has lead to undefined behavour as we get the int value based on f1.5's bit representation, 1069547520 to be precise.

Second:

B* b = new D();
D* d = static_cast<D*>(b);

Where D is derived from B. Here an implicit conversion is done in the first line. In the second line too, the cast is actually a conversion.These are conversions because the pointer value may be changed, adjusting the value if necessary to point to the complete D object or the B sub-object (I havent completely understood how offsets work though.)

Am i correct in calling pointer casts of user-defined classes conversions? If so then static_cast above has also performed a conversion(explicitly) whereas this answer i read calls casts a different concept altogether:

https://stackoverflow.com/a/34268988/1219638 -

The standard conversions are implicit conversions with built-in meanings and are separate concepts to things like static_cast or C-style casts.

Also why is a conversion of user-defined class pointer called a standard conversion?

Can i ask one last question? I have learnt that a C-style cast will act like a static_cast when the types are related. Does that mean that a C-style cast will also calculate offsets if it has to?

2
Never used anything but c-style casts in my life.user2039981
The first example is UB, see strict aliasing rule.Quimby
Please one question per question. It seems like it boils down to "what is the difference between cast and conversion" but the question could be more clear463035818_is_not_a_number
@IlianZapryanov type punning via uinions is not allowed in C++ (in C it is)463035818_is_not_a_number
There are implicit conversions and explicit conversions. Casts are explicit conversions. All conversions create new values and never change anything. Whether the representation of the new value is different from the representation of the original value is irrelevant.molbdnilo

2 Answers

5
votes

It's important to understand that, as far as the C++ language is concerned, the distinction you're talking about does not exist. All "*_casts" (and their C equivalents) are all just (explicit) conversions. And for the language, all conversions create a new object (or a new reference to an existing object if the conversion is to a reference type).

Pointers are objects. int* p2 = (int*) p1; performs a reinterpret_cast, which creates a new pointer object of a different type. p2 is a distinct object from p1, even though they are pointing to the same memory. If you did ++p2, that would in no way affect what p1 points to.

So from the perspective of the language, a cast is just certain explicit conversions that use syntax that has the word "cast" in it (or syntax equivalent to syntax with the word "cast" in it).

The distinction you're trying to make is basically when a conversion returns a value that is binary identical to the original object and when it doesn't. Well, C++ doesn't really care; all (non-reference) conversions return a new object. C++ neither has nor needs terminology to describe conversions when the new object is binary-identical to the old. And there are plenty of "*_cast" operations which may not return binary-identical values, so even if one were to desire to create such a concept, calling it a "cast" would be confusing.

Also why is a conversion of user-defined class pointer called a standard conversion?

Because that's what the C++ standard calls them. I don't mean that in the sense that we call them "standard" because they're in the C++ standard. I mean the C++ standard has a concept called "standard conversion", and conversions between certain kinds of pointers to classes are part of that.

A "standard conversion" is a conversion that can happen implicitly (ie: without specialized syntax at the site of the conversion). A pointer to a derived class can be implicitly converted to a pointer to an accessible base class. This is part of what polymorphic inheritance is for: the ability for some code to act on a derived class instance without knowing exactly which type it is acting on. The polymorphic base class provides a general interface which code is written to use, and the derived classes provide various implementations of that interface.

2
votes

I have been trying to wrap my head around the differences between type casting and type conversion.

Here is the difference:

  • Type cast is an expression. It causes a type conversion explicitly.
  • Conversions include both explicit conversions and implicit conversions.
  • Implicit conversions are not caused by casts. They occur in expressions... implicitly.

In this case it has lead to undefined behavour

Correct.

as we get the int value based on f1.5's bit representation, 1069547520 to be precise.

Note that as the behaviour is undefined, we are not guaranteed to get that precise value. Behaviour of the program could be anything; It could be crashing, failing to build, behaving exactly as you would want, or even behave exactly the way you don't want.

Am i correct in calling pointer casts of user-defined classes conversions?

It's common to call casts conversions. Although it may technically be more precise to say that casts cause conversions.

If so then static_cast above has also performed a conversion(explicitly)

Correct.

Also why is a conversion of user-defined class pointer called a standard conversion?

Because it is a pointer, and conversions between pointers are standard conversions.

Does that mean that a C-style cast will also calculate offsets if it has to?

Yes. If a static cast has to calculate an offset, and a C-style cast does a static cast, then the C-style cast calculates an offset.

P.S. Don't use C-style casts.