I managed to track a bug down to the following expression:
foo(static_cast<T>(a, b)); // Executes specialisation 1
The closing bracket was in the wrong place. The correct statement should have been:
foo(static_cast<T>(a), b); // Executes specialisation 2
I've never seen static_cast used with the form (a,b), or seen it described anywhere. What does it mean? The former statement returned b.
The static_cast operator converts variable j to type float . This allows the compiler to generate a division with an answer of type float . All static_cast operators resolve at compile time and do not remove any const or volatile modifiers.
static_cast takes the value from an expression as input, and returns that value converted into the type specified by new_type (e.g. int, bool, char, double).
static_cast is actually an operator, not a function.
Static Cast: This is the simplest type of cast which can be used. It is a compile time cast.It does things like implicit conversions between types (such as int to float, or pointer to void*), and it can also call explicit conversion functions (or implicit ones).
static_cast
is not a function, it's a keyword, so the comma in a, b
is not an argument separator; it is the comma operator. It evaluates a
but throws away the result. The expression evaluates to b
.
This has nothing to do with static_cast
, but "makes use" of the comma operator. Its result is its right hand side, so
foo(static_cast<T>(a, b));
is equivalent to
foo(static_cast<T>(b));
unless a
has other effects (which would then be executed and have their result discarded). With the right compiler settings, you will be warned about such things: Live
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