On the learncpp.com there is advice to write doubles as:
double num {5.0}
Is there any real difference between that and:
double num {5}
It seems that my compiler (VS 2019) treats those as equivalent.
For that specific case, they are effectively equivalent, however, that's not always the case.
For example:
#include <iostream>
void foo(int v) {
std::cout << "foo(int) called\n";
}
void foo(double v) {
std::cout << "foo(double) called\n";
}
int main() {
foo(5.0);
foo(5);
}
That program produces:
foo(double) called foo(int) called
If only for this reason, it's good to get into the habit of always using a double
literal when you want to express a double
value, even if an int
literal would have worked just as well. Otherwise, sooner or later, you will run into some surprises.