I am talking about "type(value)"-style casts. The books I have read pass over them quickly, saying only that they are semantically equivalent to C-style casts, "(type) value", and that they should be avoided. If they mean the same thing an old-style cast does, why were they ever added to the language? Also, because declarations can contain superfluous parentheses, this code: "T x(T(y));" doesn't do what someone intending to use the function-style casts would expect; it declares a function named x accepting a T and returning a T rather than constructing a T variable named x by casting y to a T.
Were they a mistake in the design of the language?
C-style casts can be used to convert any type into any other type, potentially with unsafe results (such as casting an integer into a pointer type). (<type>)<value> This example casts an int to a double for the purpose of avoiding truncation due to integer division: double result = (double)4/5; Popular pages.
Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); }
static_cast<>() gives you a compile time checking ability, C-Style cast doesn't. static_cast<>() is more readable and can be spotted easily anywhere inside a C++ source code, C_Style cast is'nt. Intentions are conveyed much better using C++ casts.
Typecasting is making a variable of one type, such as an int, act like another type, a char, for one single operation. To typecast something, simply put the type of variable you want the actual variable to act as inside parentheses in front of the actual variable. (char)a will make 'a' function as a char.
Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example:
template<typename T, typename U> T silly_cast(U const &u) { return T(u); }
My silly_cast
function will work for primitive types, because it's a function-style cast. It will also work for user defined types, so long as class T has a single argument constructor that takes a U or U const &.
template<typename T, typename U> T silly_cast(U const &u) { return T(u); } class Foo {}; class Bar { public: Bar(Foo const&) {}; }; int main() { long lg = 1L; Foo f; int v = silly_cast<int>(lg); Bar b = silly_cast<Bar>(f); }
The purpose of them is so you could pass more than one argument to a class' constructor:
T(a1, a2); // call 2-argument constructor (T)(a1, a2); // would only pass a2.
There is no mechanism that the (T) expr
style cast would be able to pass multiple arguments, so a new form of conversion was needed. It's natural to define (T) expr
as a degenerate case of T(expr)
.
Contrary to what some people here say, (T) expr
works exactly like T(expr)
, so it will work just fine with class types too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With