Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is or was the purpose of C++ function-style casts?

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?

like image 237
Cplusplusstudent Avatar asked Dec 17 '10 21:12

Cplusplusstudent


People also ask

What is a C-style cast?

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.

What is a function style cast?

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); }

Why is Static_cast better than C-style cast?

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.

How do you cast in C++?

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.


2 Answers

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); } 
like image 195
leedm777 Avatar answered Sep 30 '22 23:09

leedm777


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.

like image 41
Johannes Schaub - litb Avatar answered Sep 30 '22 23:09

Johannes Schaub - litb