使用clang编译TensorFlow的constexpr中的错误

问题描述 投票:0回答:2

我正在尝试使用clang编译tensorflow,并得到有关constexpr的以下错误

In file included from /tensorflow/tensorflow/lite/tools/make/downloads/absl/absl/time/clock.h:26:
/tensorflow/tensorflow/lite/tools/make/downloads/absl/absl/time/time.h:1404:55: error: constexpr function's 2nd parameter type 'std::ratio<60>' is not a literal type
constexpr Duration FromInt64(int64_t v, std::ratio<60>) {
                                        ~~~~~~~~~~~~~~^
/toolkit/include/usr/h/public/ratio:100:9: note: 'ratio<60, 1>' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors
        struct ratio
               ^

这是相关的代码段:

template <std::intmax_t N>
constexpr Duration FromInt64(int64_t v, std::ratio<1, N>) {
  static_assert(0 < N && N <= 1000 * 1000 * 1000, "Unsupported ratio");
  // Subsecond ratios cannot overflow.
  return MakeNormalizedDuration(
      v / N, v % N * kTicksPerNanosecond * 1000 * 1000 * 1000 / N);
}

constexpr Duration FromInt64(int64_t v, std::ratio<60>) {
  return (v <= (std::numeric_limits<int64_t>::max)() / 60 &&
          v >= (std::numeric_limits<int64_t>::min)() / 60)
             ? MakeDuration(v * 60)
             : v > 0 ? InfiniteDuration() : -InfiniteDuration();
}

感谢有人可以指出我正确的方向

c++ clang constexpr
2个回答
0
投票

它似乎对我来说在playground中起作用

#include <ratio>
#include <iostream>

template <std::intmax_t N>
constexpr std::intmax_t FromInt64(int64_t v, std::ratio<1, N>) {
  static_assert(0 < N && N <= 1000 * 1000 * 1000, "Unsupported ratio");
  return N;
}

constexpr std::intmax_t FromInt64(int64_t v, std::ratio<60>) {
  return -100;
}

int main() {
    auto a = FromInt64(5, std::ratio<1, 5>{});
    std::cout << a << " "; // 5
    auto b = FromInt64(5, std::ratio<60>{});
    std::cout << b; // -100
}

0
投票

[可能是在该上下文中尚未定义std::ratio。如果std::ratio不完整,则代码将无法编译。也许尝试

#include <ratio> 

在编译该代码之前。

© www.soinside.com 2019 - 2024. All rights reserved.