0

When I cast a float to an int, for example:

float f = 32.4;
int i = (int)f;

The raw bits of f are not copied directly into i, but rather the bits that represent 32 are copied into i.

If the int casting worked differently and only copied the raw bits of f into i, does it still called casting? Or does casting means that the least amount of information that can be represented by the assigned type (int in this case) must be copied?

4 Answers4

2

What you've shown is still called casting. If you want to copy the raw bits, you can use memcpy:

int i;
static_assert(sizeof(i) == sizeof(f), "oops");
memcpy(&i, &f, sizeof(i));
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

A C cast can do any of static_cast, reinterpret_cast or const_cast, or even a combination of two such, depending on the context. And that's why it's very dangerous notation: its meaning can change very unexpectedly when the code is maintained. A C cast can even give you access to an inaccessible base, which no named cast can.

A reinterpret_cast is intended to keep the bits, although that's up to the implementation.

It can only cast pointers and references. However, that may not have been the 1original intention, and it's easily circumvented by just writing e.g. reinterpret_cast<U&>( t_instance ). Anyway, when you do a reinterpret_cast you're usually in formally Undefined Behavior land, but relying on platform- and/or compiler guarantees: you are taking the responsibility for the portability of your code, if any, and not leaving that to the standard's guarantees.


1) Judging by an attempted but invalid reinterpret_cast in some code that I once had private access to, i.e., this is an informed opinion.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

The word cast is the name of a syntax construct. These are all casts:

(any_typename) foo
static_cast<any_typename>(foo)
reinterpret_cast<any_typename>(foo)

A cast may also be called an explicit conversion. Conversions that are not written with cast notation are called implicit.

Conversions may or may not be implementable as a "raw bits copy". The conversion int i = f; (with or without the cast operator - makes no difference) can't be. However, long x = 5; int i = x; could be, if int and long are the same size on that platform.

Another conversion that could be a raw bit copy is char *ptr = "hello"; void *q = ptr;. In general, other pointer conversions might not be raw bit copies.

Conversions between user-defined types (i.e. class types) can only occur if the class has a conversion operator or converting constructor defined, and in this case you write code that specifies how the conversion is performed.

M.M
  • 138,810
  • 21
  • 208
  • 365
0

According to the explanation found on Wikipedia, 'casting' means that a type conversion takes place. Depending on the type and the definition of the cast, this might be just copying of the raw data as well as a sophisticated interpretation mechanism. There is no requirement for the cast to work in either way; however a copy of the raw data usually is not the desired behaviour.

Codor
  • 17,447
  • 9
  • 29
  • 56
  • 1
    why use Wikipedia when there's a perfectly good C++ standard? – M.M Jan 21 '15 at 07:14
  • @MattMcNabb Good point. This document http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf Subsection 6.5.4 ("Semantics") mentions that a cast operator 'converts the value'. Does this mean that a mere copy of the bits is ruled out? – Codor Jan 21 '15 at 07:28
  • 1
    The identity conversion is still a conversion :) – M.M Jan 21 '15 at 07:30
  • 1
    That's the C standard BTW; for links to C++ drafts [see here](http://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – M.M Jan 21 '15 at 07:35
  • Thanks for the link. However, is my answer still correct in the sense that, especially as the cast operator can be overloaded in C++, neither interpretation of the value nor a bitwise copy is completely ruled out? – Codor Jan 21 '15 at 07:39
  • by "interpretation" I think you mean a conversion other than a bitwise copy. (The word "reinterpret" means something else in C++) – M.M Jan 21 '15 at 07:55
  • Yes exactly; by "interpretation" I mean "calculation of a value that is not a bitwise copy and tries to represent the value to be converted in a meaningful way" (as in a cast from `float` to `int`). – Codor Jan 21 '15 at 08:03