Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Significance of -Werror=old-style-cast?

Tags:

c++

I'm using code that casts some ints to floats for division.

size_t a;
uint8_t b, c;

a = (float)b / (float)c;

I was compiling with warning flags enabled and I got one for 'old cast'. Is there a better or proper way I should be casting these things? If so, how?

like image 404
user3816764 Avatar asked Feb 13 '23 10:02

user3816764


1 Answers

Old style casts are "C-style" casts. -Werror=old-style-cast turns the usage of C-style casts into errors. You should use the C++ casts.

Here you can use a static_cast :

size_t a; uint8_t b, c;

a = static_cast<float>(b) / static_cast<float>(c);
like image 177
quantdev Avatar answered Feb 15 '23 12:02

quantdev