【问题标题】:When would std::common_type be used with a single argument?何时将 std::common_type 与单个参数一起使用?
【发布时间】:2018-01-11 12:38:22
【问题描述】:

我注意到libcxx 中实现的std::chrono::duration 的一元+ 和- 运算符的返回类型为typename common_type<duration>::type。为什么它不像其他算术运算符那样简单地duration

【问题讨论】:

  • p0548r1 有一些有用的参考资料,可能会有所启发

标签: c++ duration chrono


【解决方案1】:

这是 C++17 的更改。文档p0548r1 调整了operator+operator- 的返回类型,以便duration 包含:

constexpr common_type_t<duration> operator+() const; 
constexpr common_type_t<duration> operator-() const;

显然common_type_t 等价于common_type&lt;duration&gt;::type。其基本原理是与二进制operator+operator- 保持一致。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-25
    • 2014-08-30
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    相关资源
    最近更新 更多