converting double to int

S

Stefan Ram

#include <iostream>
#include <ostream>

int main()
{ ::std::cout << int( 2.6 )<< '\n';
::std::cout << int{ 2.6 }<< '\n';
::std::cout << ( int )2.6 << '\n';
::std::cout << static_cast< int >( 2.6 )<< '\n'; }

Under which circumstances is which way of the above to
convert a double to an int the best style?

Are there any differences in the semantics above?

Is there yet another way to write the conversion?

PS: Oh yeah: the int{} notation forbids narrowing!
I should have compiled with -pedantic-errors!
So, int{} differs from the rest, it is not a kind of
cast or conversion, rather a compile-time assertion
of the number being not wider than int?

Is the number »2.6« an argument or an operand in
»int{ 2.6 }«? It does not seem to be a function
call. So it is not an argument. But is this an
application of an operator? No. So it also is not
an operand? So, what is it then?
 
A

Alf P. Steinbach

#include <iostream>
#include <ostream>

int main()
{ ::std::cout << int( 2.6 )<< '\n';
::std::cout << int{ 2.6 }<< '\n';
::std::cout << ( int )2.6 << '\n';
::std::cout << static_cast< int >( 2.6 )<< '\n'; }

int( 2.6) means exactly the same as (int)2.6. It's a C style cast. C++
added the first notation, with meaning defined to be the same as the second.

In the context at hand the C style cast reduces to C++ static_cast<int>,
but more generally what it reduces to depends on the source and
destination types. It can cast away constness, or do a reinterpret_cast,
or cast to an otherwise inaccessible base class, and it can even do two
things at once. So with code that's going to be maintained, it's a good
idea to generally stay away from C style casts.

Under which circumstances is which way of the above to
convert a double to an int the best style?

Are there any differences in the semantics above?

Is there yet another way to write the conversion?

PS: Oh yeah: the int{} notation forbids narrowing!
I should have compiled with -pedantic-errors!
So, int{} differs from the rest, it is not a kind of
cast or conversion, rather a compile-time assertion
of the number being not wider than int?

C++11 brace initialization does not permit narrowing.

Is the number »2.6« an argument or an operand in
»int{ 2.6 }«? It does not seem to be a function
call. So it is not an argument. But is this an
application of an operator? No. So it also is not
an operand? So, what is it then?

while int( 2.6 ) is a C style cast it's also a "pseudo constructor
call", a notation that looks like an invocation of a constructor and has
the same effect as such a call would have had, if the int type had had
any constructors. That's mostly to provide a unified notation for use in
templated code. However, the standard does not, or did not in C++03, use
the term "pseudo constructor call", but it does (or did) use the
corresponding term "pseudo destructor call", IIRC.

With braces the expression is just the same except that the C++11 rules
for braces initialization come into play, prohibiting the narrowing.

For the general effect C11 §4.9/1 "Floating-integral conversions" states
that

A prvalue of a floating point type can be converted to a prvalue of an
integer type. The conversion truncates; that is, the fractional part is
discarded. The behavior is undefined if the truncated value cannot be
represented in the destination type

So for a general conversion you'd better check the limits, e.g. via
INT_MIN and INT_MAX from the C library's <limits.h>, or via C++
std::numeric_limits<int>::min() and max() from <limits>. Unfortunately
such narrowing conversion is not part of the standard library. There is
a good chance, however, that you will some usable function in Boost.


Cheers & hth.,

- Alf (hoping that all these questions of yours which look quite a bit
like homework, are not?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,930
Messages
2,570,072
Members
46,521
Latest member
JamieCooch

Latest Threads

Top