In section 10.5.1 of Stroustrup's new book "The C++ Programming Language - Fourth Edition" he says, that before an arithmetic operation is performed, integral promotion is used to create ints out of shorter integer types, and similarly, floating-point promotion is used to create doubles out of floats.
I confirmed the first claim with the following code:
#include <iostream>
#include <typeinfo>
int main()
{
short a;
short b;
std::cout << typeid(a + b).name() << std::endl;
}
This outputs "int" with vc++ and "i" with gcc.
But testing it with floats instead of shorts, the output is still "float" or "f":
#include <iostream>
#include <typeinfo>
int main()
{
float a;
float b;
std::cout << typeid(a + b).name() << std::endl;
}
According to Stroustrup there are no exceptions to the floating-point promotion-rule, so I expected "double" or "d" as output.
Is the mentioned section about promotions wrong or somehow unclear? And is there any difference in C++98 and C++11 regarding type promotions?
float
todouble
only occurs in case of mixed arithmethic; that is when adding afloat
and adouble
together thefloat
is first promoted before the addition is carried out. Are you sure it is not the case ? – Rosierosilyint
is the "minimum level" for integral arithmetic in C++, andfloat
is the "minimum level" for floating-point arithmetic. – Toolispg 267: s/Similarly, floating-point promotion is used to create doubles out of floats//
(for those unfamiliar with that syntax, it means that sentence should be removed) – Spoony