From: Goran on
On Feb 9, 5:07 am, 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.

Ok, then, is "long whatever" yours? ;-)

> 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?

Seriously, compiler is telling you that something might be wrong. But
it has no idea if that's true and if yes, why - that's your problem.

If you just cast, you are telling the compiler "I know that this
particular long never has values outside [INT_MIN, INT_MAX]" (but then
you might try to use int yourself, not long). If you can't say that,
you should take a precautionary step. What would that be, __hugely__
depends on the context. An assert, exception, log-and-continue come to
mind as a course of action.

There is IMO __no__ easy way out.

Goran.


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

From: Sirius Black on
On Tue, 09 Feb 2010 12:43:53 -0600, Francis Glassborow wrote:

<snip>

> 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.

Why return? Even without return the behavior is same, is n't it?

~ Sirius


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