libc++ 持续时间隐藏警告不正确

libc++ duration hiding warnings incorrectly?

本文关键字:不正确 警告 隐藏 持续时间 libc++      更新时间:2023-10-16

以下代码在没有警告的情况下工作:

std::chrono::duration<unsigned int> d{-17};

我希望收到与我相同的警告:

unsigned int x = -17;

以下是相关代码:

template<typename _Rep2, typename = typename
        enable_if<is_convertible<_Rep2, rep>::value
                  && (treat_as_floating_point<rep>::value
                  || !treat_as_floating_point<_Rep2>::value)>::type>
explicit duration(const _Rep2& __rep)
    : __r(static_cast<rep>(__rep)) { }

static_cast隐藏了警告,在我看来,标准要求的任何功能都不需要它。这只是一个libc++问题,还是标准要求以这种方式工作?

这是标准预期的行为。对该构造函数的注释是:

此构造函数不应参与重载解析,除非Rep2 可转换为rep
(1.1) — treat_as_floating_point_v<rep> true
(1.2) — treat_as_floating_point_v<Rep2> false .

int隐式转换为unsigned inttreat_as_floating_point<int> false,所以我们很好。

效果是:

后置条件:count() == static_cast<rep>(r)

libc++ 和 libstdc++ 都通过允许你编写的代码来符合要求。它的格式良好。如果您认为它的格式不正确,则应提交有关它的问题。这不是编译器错误。这可能是一个标准错误。