【问题标题】:Is static_cast on bounded types implementation-dependent?有界类型上的 static_cast 是否依赖于实现?
【发布时间】:2017-03-09 02:32:51
【问题描述】:

我在看static_cast with bounded types

是特定于行为的吗?换句话说(给定 16 位 short 和 32 位 long)是

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

保证产生 y = 4464(x 的低 16 位)?还是只在 little-endian 机器上?

我一直认为它会,但我在大端机器上得到奇怪的结果并试图弄清楚它们。

这是实际问题。我有两个 time_t(大概是 64 位),我“知道”它们总是在合理的秒数内。我想用printf展示差异。该代码是多平台的,因此我不必担心 time_t 的底层类型是什么,而是通过 static_cast(time2-time1) 执行 printf("%d")。尽管 printf 位于以 (time2 != time1) 为条件的块中,但我看到的是零。 (printf 在库中;没有合理的可能性使用 cout 代替。)

static_cast 是否可能返回 time_t 的高 32 位?

有没有更好的方法来做到这一点?

谢谢,

【问题讨论】:

  • 很奇怪。我输入 static_cast angle-bracket int anglebracket (time2-time1) 但显示的只是 static_cast(time2-time1)。我需要以 static_cast\\>(time2-time1) 或类似的方式转义吗?
  • 已解决。需要使用 & 语法。

标签: static-cast endianness


【解决方案1】:

我认为问题可能与 static_cast 无关。 #ifdef 平台混乱。如果有人明确知道答案,我仍然会感兴趣。

【讨论】:

    猜你喜欢
    • 2011-08-24
    • 2021-12-28
    • 1970-01-01
    • 2016-09-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-05
    • 2014-04-21
    • 1970-01-01
    相关资源
    最近更新 更多