Nice way to cast a homogeneous tuple

Ulrich Eckhardt eckhardt at satorlaser.com
Thu Jul 29 03:21:37 EDT 2010


Steven D'Aprano wrote:
> Perhaps I have been misinformed, but my understanding of C type-casts is
> that (where possible), a cast like `int(var)` merely tells the compiler
> to temporarily disregard the type of var and treat it as if it were an
> int. In other words, it's a compiler instruction rather than a conversion
> function.

You are misinformed. The result of a cast in C or C++ behaves as if a
temporary was created:

  int x = 0;
  unsigned(x)--; // invalid, compiler error

Now, where this distinction gets blurred is when you are casting pointers:

  (*(unsigned*)&x)--;

or, in C++, references:

  reinterpret_cast<unsigned&>(x)--;

Technically, these are still invalid though, only that they give you
undefined behaviour at runtime instead of a compiler error, but those are
already very fine details of the according standards.


Uli

-- 
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932




More information about the Python-list mailing list