Skip to content

durations of floating point type or smaller than long long cause "conditional expression is constant" on MSVC #2854

Description

@lefticus

/W4 is required. Seems to happen in any version of {fmt}, cannot reproduce in GCC or Clang

They appear to be similar but unrelated warnings.

Here is a minimal repro:

https://compiler-explorer.com/z/Yve79WaTf

#include <fmt/format.h>
#include <fmt/chrono.h>
#include <chrono>

int main()
{
  // this works
  //using milliseconds = std::chrono::duration<long long, std::milli>;

  // but this does not
  //using milliseconds = std::chrono::duration<int, std::milli>;

  // and neither does this
  using milliseconds = std::chrono::duration<double, std::milli>;

  fmt::print("ms {}", milliseconds{});
}

Warning stems from this line:

fmt/include/fmt/chrono.h

Lines 246 to 260 in d141cdb

if (Factor::num != 1) {
constexpr auto max1 = detail::max_value<IntermediateRep>() /
static_cast<IntermediateRep>(Factor::num);
if (count > max1) {
ec = 1;
return {};
}
constexpr auto min1 = std::numeric_limits<IntermediateRep>::lowest() /
static_cast<IntermediateRep>(Factor::num);
if (count < min1) {
ec = 1;
return {};
}
count *= static_cast<IntermediateRep>(Factor::num);
}

and this line:

fmt/include/fmt/chrono.h

Lines 82 to 87 in d141cdb

if (F::digits > T::digits &&
from > static_cast<From>(detail::max_value<To>())) {
ec = 1;
return {};
}
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions