0

I'm looking at static_cast with bounded types .

Is the behavior implementation-specific? In other words (given 16-bit shorts and 32-bit longs) is

long x = 70000;
short y = static_cast<short>(x);

guaranteed to produce y = 4464 (the low-order 16 bits of x)? Or only on a little-endian machine?

I have always assumed it would but I am getting odd results on a big-endian machine and trying to figure them out.

Here's the actual problem. I have two time_t's (presumably 64 bits) that I "know" will always be within some reasonable number of seconds of each other. I want to display that difference with printf. The code is multi-platform, so rather than worry about what the underlying type of time_t is, I am doing a printf("%d") passing static_cast<int>(time2-time1). I'm seeing a zero, despite the fact that the printf is in a block conditioned on (time2 != time1). (The printf is in a library; no reasonable possibility of using cout instead.)

Is static_cast possibly returning the high 32 bits of time_t?

Is there a better way to do this?

Thanks,

Community
  • 1
  • 1
Charles
  • 479
  • 1
  • 3
  • 13
  • Weird. I type static_cast angle-bracket int anglebracket (time2-time1) but what displays is just static_cast(time2-time1). Do I need to escape as static_cast\\>(time2-time1) or something like that? – Charles Oct 26 '16 at 16:28
  • Solved. Need to use ampersand syntax. – Charles Oct 26 '16 at 16:35

1 Answers1

0

I think perhaps the problem was unrelated to the static_cast. #ifdef platform confusion. I'd still be interested if someone definitively knows the answer.

Charles
  • 479
  • 1
  • 3
  • 13