error C2440 type cast from double to int

  • Thread starter Thread starter TomLigon
  • Start date Start date
T

TomLigon

Guest
Hello,

what I am looking for is some expert advice on a question, and if possible a link to the C++ standard documentation (i.e. current standard, not vendor specific). I am using Visual Studio 2017 C++ to compile C++ for MATLAB (mex). MATLAB supports Visual Studio 2017, but seems to prefer MinGW, and my colleagues say that Visual Studio is "bad", and they only use MinGW. So, I want an expert opinion on the current question. One line of code causes error C2440, and I have written a test program to reproduce it.

int main()
{
double A = 1.1;
int B = 0;
enum firstType
{
type1 = 1,
type2 = 2
};
firstType C = type2;
B = A; /* line 11, warning C4244 */
B = (int)A; /* OK */
C = (firstType)A; /* line 13, error C2440 */
C = (firstType)int(A); /* OK */
C = (firstType)static_cast<int>(A); /* OK */
C = static_cast<firstType>(A); /* line 16, error C2440 */
}

Line 11 causes warning C4244

Lines 13 and 16 cause error C2440

Lines 14 and 15 are 2 ways to avoid the error in Visual Studio 2017.

However, line 13 presumably works fine in MinGW.

Why is line 13 flagged as an error in VS? Is there any "official" reason why it is wrong?



Tom

Continue reading...
 
Back
Top