Recently, I am confused with this two expression - (type)value
and type(value)
.
For example : size_t c = size_t(-1); size_t c = (size_t)-1;
what is the difference?
Recently, I am confused with this two expression - (type)value
and type(value)
.
For example : size_t c = size_t(-1); size_t c = (size_t)-1;
what is the difference?
The former is C++, it's doing direct initialization by calling a constructor.
The second is C (or C++), it's doing a cast.
So, there's a lot of difference from a C programmer's point of view.
Also, this is a pretty bad idea, since size_t
is an unsigned type. This should be done using ssize_t
, which is signed and removes the need for the cast.
In C, only one of these is a valid expression.
In C++, they are exactly the same, they are two alternative forms of writing an explicit cast expression: C-style and the functional style.