ISO: WG21/N0502 ANSI: 94-0115 Author: John Max Skaller Date: May 1994 Reply to: maxtal@suphys.physics.su.oz.au "IMPLICIT NEW STYLE CAST" ------------------------- The xxxxx_cast<>() family is not complete, because there is no member of the family for implicit conversions. Proposal -------- The new style cast: implicit_cast<>() is added to the new style cast family. A program may use a new style implicit conversion cast between types that may be implicitly converted, any other conversion is ill-formed. A new style cast may also be used to select a function pointer or pointer to member function from a set of overloaded functions: void f(int); void f(char); long x; f(x); // ambiguous f( implicit_cast(x) ); // fine but .. f( implicit_cast(x) ); // woops! didnt mean that (*implicit_cast(f)) (x); // fine (*implicit_cast(f)) (x); // ill formed This is useful if the function is an argument to another function .. and both are overloaded. void g( void (*)(int)); void g( void (*)(char)); g(f); // ambiguous g( implicit_cast(f) ); // cast argument (*implicit_cast(g)) (f); // cast function Importance ---------- This cast is important for several reasons. First, it completes the cast set, so that all old style casts can be written as new style casts. Second, unlike the function style casts, it can be used to perform an implicit conversion to a type that does not have an identifier as a name. For example: char *x; implicit_cast(x); The equivalent function style cast requires a typedef: typedef const char* ccharp; ccharp(x); which is ugly, pollutes the global namespace, and cant be done directly inside an expression: that is, it fails to supply the lexical locality it should, old style casts provide that but without validating the cast is indeed "safe": char const *x; *implicit_cast(l)=1; // ill formed *(char*)(l)=1; // undefined behaviour Explicitly writing out implicit casts is useful because they make the operation visible. But there is a more important semantic difference, they make the operation literal: typedef char byte; typedef long word; word w; byte b = w; //#1 truncate to 8 bits byte b = implicit_cast(w); //#2 truncate to 8 bits If we change the typedef: typedef long byte; then #1 is a lie, but #2 remains true.