c++ - std::chrono::duration 的运算符/和带有 clang 的自定义类型

标签 c++ clang c++-chrono

考虑一个自定义类型,它用于乘除持续时间的特定实例:

#include <chrono>
#include <iostream>

class Foo {};

using Duration = std::chrono::seconds;

inline Duration operator*(Duration d, Foo) {
    std::cout << "multiplying some time with Foo\n";
    return d;
}

inline Duration operator/(Duration d, Foo) {
    std::cout << "dividing some time by Foo\n";
    return d;
}

int main() {
    Duration d;
    Foo f;
    d * f;
    d / f;
}

此代码使用 gcc 编译时没有警告,但使用 clang ( wandbox) 时失败

In file included from prog.cc:1:
/opt/wandbox/clang-7.0.0/include/c++/v1/chrono:1259:81: error: no type named 'type' in 'std::__1::common_type<long long, Foo>'
                          typename common_type<typename _Duration::rep, _Rep2>::type>::value>
                          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~
/opt/wandbox/clang-7.0.0/include/c++/v1/chrono:1272:7: note: in instantiation of default argument for '__duration_divide_imp<std::__1::chrono::duration<long long, std::__1::ratio<1, 1> >, Foo>' required here
    : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/wandbox/clang-7.0.0/include/c++/v1/chrono:1279:10: note: in instantiation of template class 'std::__1::chrono::__duration_divide_result<std::__1::chrono::duration<long long, std::__1::ratio<1, 1> >, Foo, false>' requested here
typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
         ^
prog.cc:22:7: note: while substituting deduced template arguments into function template 'operator/' [with _Rep1 = long long, _Period = std::__1::ratio<1, 1>, _Rep2 = Foo]
    d / f;

请注意,operator* 在两种编译器上都能正常工作。

实际代码有点复杂,使用在类作用域类型中定义的友元方法,该方法对持续时间执行溢出安全整数操作,但表现出完全相同的症状。

问题类似于:User-defined overloaded operator * with std::chrono::duration ,但这是不同的运算符和编译器。

最佳答案

这对我来说看起来像一个 libc++ 错误(也是我写的一个错误)。这是一个经过非常简单测试的修复程序:

--- a/include/chrono
+++ b/include/chrono
@@ -1289,7 +1289,12 @@ struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
 template <class _Rep1, class _Period, class _Rep2>
 inline _LIBCPP_INLINE_VISIBILITY
 _LIBCPP_CONSTEXPR
-typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
+typename enable_if
+<
+    !__is_duration<_Rep2>::value &&
+    is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
+    duration<typename common_type<_Rep1, _Rep2>::type, _Period>
+>::type
 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
 {
     typedef typename common_type<_Rep1, _Rep2>::type _Cr;

关于c++ - std::chrono::duration 的运算符/和带有 clang 的自定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55167862/

相关文章:

c++ - Windows Visual C++ 链接到 Mesa3D OpenGL

c - 使用 typeof 编译器扩展在 C 中实现 min 函数会出现错误

c++ - 为什么 clang 和 gcc 以不同的方式处理具有类内初始化的结构的支撑初始化?

debugging - 我可以将哪些标志或环境变量传递给 Clang 以在 BSD 和 Linux 上进行最大程度的调试?

c++ - Chrono,c++,比较日期

c++ - 如何在 double 中存储 chrono 的时间?

c++ - 如何将 YYYY/MM/DD HH:MM:SS 转换为 std::chrono::system_clock::time_point?

c++ - 在 unordered_set 中插入一个新元素 : should the hint be end()?

c++ - C 接口(interface)是否关心指向的类型?

c++ - Vim omnicppcomplete 'using namespace' 问题