Static cast by value

Aug 21, 2006 17:31

In C++, static_cast(some_double) converts some_double to an int. In general, C++-style casts are safer than the C-style casts because they make explicit the intent of the cast. However, when converting a double to an int, is there any reason to use static_cast rather than using int's constructor: int(some_double)?

I see static_cast used like this ( Read more... )

cpp, lazyweb, c++

Leave a comment

Comments 2

thegreatgonz August 22 2006, 00:23:22 UTC
I have heard it argued that C++ casts are better because you can grep for them, but how often do you need to search for all the places you convert doubles to ints?

If you care about correctness more than performance, boost::numeric_cast seems like it might be the right choice. Otherwise, I'd say go for the constructor syntax.

Reply

benfrantzdale August 22 2006, 15:11:19 UTC
Good point about numeric_cast.

I agree with the grepping argument for C++ casts but I think I like them most because they reflect the intention of the programmer. I also like C++-style casts because they are verbose (since casting is bad, casts should look kludgy).

I think I'll stick with the constructor form.

Reply


Leave a comment

Up