Is `static_cast<X>(y)` the same as `X(y)` for simple data types like `int` and `double`?
casting - In C++, what are the differences between static_cast (a) and double(a)? - Stack Overflow
Rounding with static_cast<int>? - c++
Number Type Cast
Say I had the lines:
double d = 1.1; auto a = int(d); auto b = static_cast<int>(d);
I understand that I'm doing a cast of a double to an in for the creation of variable b, but is that also happening as well for the creation of a? If I can use is as a shorthand for doing static casts, I'd love to. But if that constructor style variable creation isn't actually doing a static cast, I'd like to know too.
EDIT:
I would say this is my favourite answer of the bunch: https://www.reddit.com/r/cpp_questions/comments/i76d6e/is_static_castxy_the_same_as_xy_for_simple_data/g119fnu/
Someone may have thought they were constructing rather than casting. Consider:
some_fun(std::string("Hello"));
Many people think they're calling a constructor there when in fact they're doing a C-style cast. It just so happens that casting will look at constructors of the target type among the long list of other things it looks at and so here it eventually ends up invoking the constructor.
Functional notation casts have all the same weaknesses of the other kind of C cast:
- Can inadvertently cast away constness
- Can silently turn into a reinterpret cast
- Are hard to differentiate with grepping tools.
Besides all that though, you're performing exactly the same operation in both cases.
The latter is referred to as the functional notation of explicit casting where you explicitly say a should be treated as a double. You can pretty much cast anything to any type using this technique.
The former is the preferred way to cast a type in C++. It does basic checking to see that the type you are casting to makes sense (child class pointer to a base class pointer, etc.). In addition, like in the example you show, you can perform implicit conversions. Technically the static_cast in your example is explicit, but the result of the operation (the assignment) is implicit.
You should use reinterpret_cast for casting pointers, i.e.
r = reinterpret_cast<int*>(p);
Of course this makes no sense,
unless you want take a int-level look at a double! You'll get some weird output and I don't think this is what you intended. If you want to cast the value pointed to by p to an int then,
*r = static_cast<int>(*p);
Also, r is not allocated so you can do one of the following:
int *r = new int(0);
*r = static_cast<int>(*p);
std::cout << *r << std::endl;
Or
int r = 0;
r = static_cast<int>(*p);
std::cout << r << std::endl;
Aside from being pointers, double* and int* have nothing in common. You could say the same thing for Foo* and Bar* pointer types to any dissimilar structures.
static_cast means that a pointer of the source type can be used as a pointer of the destination type, which requires a subtype relationship.
I can not get a simple conversion from int to double to work
Actually, the conversion itself works just fine.
d1 and d2 equals 8, instead of 8.0
8 and 8.0 are two ways of representing the same value as a string. There is no difference in the value.
AFAIK, d1 and d2 should be 8.0, shouldn't then?
They are, because 8 and 8.0 are the same exact value.
Can anyone, please, tell me what am I doing wrong?
Your mistake is expecting the output to be 8.0. The correct output is 8, because of the default formatting settings of the output stream.
Another mistake is assuming that there is a problem with the conversion from int to double, when the problem is actually in the formatting of the textual output.
If your intention was to output 8.0, then your mistake was to not use the correct stream manipulators to achieve that format. In particular, you need to use the std::fixed manipulator to show fixed number of decimals, even when they are zero. You can use std::setprecision to set the number of decimals to show. Depending on how you want the output formatted in different cases, std::showpoint might also be an option.
Make no mistake, d1 and d2 are doubles. You need std::setprecision() (and apparently std::fixed) too found in the header <iomanip>:
#include <iostream>
#include <iomanip>
int main()
{
int i = 8;
double d1 = i;
double d2 = static_cast<double>(i);
std::cout << "i = " << i << std::fixed << std::setprecision(2) << ", d1 = " << d1 << ", d2 = " << d2 << std::endl;
return 0;
}