BufBills
BufBills

Reputation: 8113

Is this static_cast valid/legit?

double diff = static_cast<int64_t>(a- b);

a and b are of type int64_t.

I saw this code in our project. I think it is suspicious, but I am really not sure. I am familiar with static_cast, and I would not write code like this.

Is this static_cast valid/legit? Is it useful?

Upvotes: 0

Views: 99

Answers (2)

bames53
bames53

Reputation: 88225

This cast is valid but does not do anything. I agree that it is suspicious. You should carefully review what the code is supposed to be doing, and if it's correct then you may want to re-write it so that the intent is clearer and so that it doesn't look as suspicious.

Upvotes: 0

Zac Howland
Zac Howland

Reputation: 15870

If a and b are both int64_t, there is no point to the cast. It is casting a result of type int64_t to type int64_t. It would be like doing this:

int a = 10, b = 5;
double c = (int)(a - b); // the cast is not needed, but also not "harmful"

Upvotes: 1

Related Questions