命名空间
变体
操作

std::literals::chrono_literals::operator""h

来自 cppreference.cn
< cpp‎ | chrono
 
 
 
 
定义于头文件 <chrono>
constexpr std::chrono::hours
    operator""h( unsigned long long hrs );
(1) (自 C++14 起)
constexpr std::chrono::duration</*unspecified*/, std::ratio<3600,1>>
    operator""h( long double hrs );
(2) (自 C++14 起)

形成一个 std::chrono::duration 字面量,表示小时。

1) 整数字面量,精确返回 std::chrono::hours(hrs)
2) 浮点数字面量,返回等价于 std::chrono::hours 的浮点数持续时间。

目录

[编辑] 参数

hrs - 小时数

[编辑] 返回值

std::chrono::duration 字面量。

[编辑] 可能的实现

constexpr std::chrono::hours operator""h(unsigned long long h)
{
    return std::chrono::hours(h);
}
 
constexpr std::chrono::duration<long double, ratio<3600,1>> operator""h(long double h)
{
    return std::chrono::duration<long double, std::ratio<3600,1>>(h);
}

[编辑] 注解

此运算符声明于命名空间 std::literals::chrono_literals 中,其中 literalschrono_literals 都是内联命名空间。可以通过以下方式访问此运算符:

  • using namespace std::literals,
  • using namespace std::chrono_literals,或者
  • using namespace std::literals::chrono_literals.

此外,在命名空间 std::chrono 中,标准库提供了指令 using namespace literals::chrono_literals;,因此,如果程序员使用 using namespace std::chrono; 来访问 chrono 库中的类,则相应的字面量运算符也将变为可见。

[编辑] 示例

#include <chrono>
#include <iostream>
 
int main()
{
    using namespace std::chrono_literals;
    auto day = 24h;
    auto halfhour = 0.5h;
    std::cout << "one day is " << day.count() << " hours (" << day << ")\n"
              << "half an hour is " << halfhour.count() << " hours ("
              << halfhour << ")\n";
}

输出

one day is 24 hours (24h)
half an hour is 0.5 hours (0.5h)

[编辑] 参见

构造新的 duration
(std::chrono::duration<Rep,Period> 的公有成员函数) [编辑]