From: Joseph Bentley on
This code produces a warning regarding "conversion of long type to int
type".
....
void funk(int);
....
long whatever;
....
funk(whatever); // warning here
....

Of course, I know that I should avoid these situations when I can,
like making funk()'s argument a long. But sometimes funk() is not
your function, and you must live with the int argument. So my
question is, should I suppress the warning, or is there some cast I
don't know about to disguise the problem, some another approach, or
forget about it?

Thanks,
Joe

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Joseph Bentley wrote:
> This code produces a warning regarding "conversion of long type to int
> type".
> ...
> void funk(int);
> ...
> long whatever;
> ...
> funk(whatever); // warning here
> ...
>
> Of course, I know that I should avoid these situations when I can,
> like making funk()'s argument a long. But sometimes funk() is not
> your function, and you must live with the int argument.

Well, you could also change 'whatever' to an int.

> So my question is, should I suppress the warning, or is there some
> cast I don't know about to disguise the problem, some another approach,
> or forget about it?

IMHO there are three solutions:
1. Ignore the warning for now, after all it is just a warning.
2. Add a runtime-checked conversion which checks that the conversion is
lossless or signals the error.
3. Add an explicit conversion (static_cast) if you know that the long's
range doesn't exceed the int's, possibly paired with an assertion that it
works without losing any bits.

Uli

--
Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Joseph Bentley wrote:

> This code produces a warning regarding "conversion of long type to int
> type".
> ...
> void funk(int);
> ...
> long whatever;
> ...
> funk(whatever); // warning here
> ...
>
> Of course, I know that I should avoid these situations when I can,
> like making funk()'s argument a long. But sometimes funk() is not
> your function, and you must live with the int argument. So my
> question is, should I suppress the warning, or is there some cast I
> don't know about to disguise the problem, some another approach, or
> forget about it?
>

I would use a static_cast:

f(static_cast<int>(whatever));



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Manan on
On Feb 8, 10:07 pm, Joseph Bentley <joe.foxho...(a)gmail.com> wrote:
> This code produces a warning regarding "conversion of long type to int
> type".
> ...
> void funk(int);
> ...
> long whatever;
> ...
> funk(whatever); // warning here
> ...
>
> Of course, I know that I should avoid these situations when I can,
> like making funk()'s argument a long. But sometimes funk() is not
> your function, and you must live with the int argument. So my
> question is, should I suppress the warning, or is there some cast I
> don't know about to disguise the problem, some another approach, or
> forget about it?

{ quoted thanks and banner removed -mod }

You can use funk(static_cast<int>(whatever))



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Francis Glassborow on
Joseph Bentley wrote:
> This code produces a warning regarding "conversion of long type to int
> type".
> ...
> void funk(int);
> ...
> long whatever;
> ...
> funk(whatever); // warning here
> ...
>
> Of course, I know that I should avoid these situations when I can,
> like making funk()'s argument a long. But sometimes funk() is not
> your function, and you must live with the int argument. So my
> question is, should I suppress the warning, or is there some cast I
> don't know about to disguise the problem, some another approach, or
> forget about it?
>
> Thanks,
> Joe
>

You might try producing a checking overload of funk to catch cases where
whatever is out of the int range.

void funk(long value){
if (value > INT_MAX || value < INT_MIN) throw range_error;
return funk( static_cast<int> value );
}

And that return is fine, the standard is carefully crafgted to allow
return of void in cases like this one.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]