名前空間
変種
操作

std::chrono::treat_as_floating_point

提供: cppreference.com
< cpp‎ | chrono
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (C++20)
(C++11)
関係演算子 (C++20で非推奨)
整数比較関数
(C++20)
スワップと型操作
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
一般的な語彙の型
(C++11)
(C++17)
(C++17)
(C++17)
(C++17)

初等文字列変換
(C++17)
(C++17)
 
日付と時間のユーティリティ
(C++11)
(C++11)
時刻
(C++20)



(C++20)(C++20)(C++20)(C++20)
時計
(C++20)
                                             
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
カレンダー
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
タイムゾーン
(C++20)
(C++20)
(C++20)
(C++20)
C スタイルの日付と時間
 
 
ヘッダ <chrono> で定義
template <class Rep>
struct treat_as_floating_point : std::is_floating_point<Rep> {};
(C++11以上)

std::chrono::treat_as_floating_point 特性は、 duration が異なる刻み幅を持つ別の duration に変換可能かどうかの判定を補助します。

2つの duration 間の暗黙の変換は、通常、その duration の刻み幅に依存します。 しかし、 std::chrono::treat_as_floating_point<Rep>::value == true であれば、刻み幅によらず、暗黙の変換が発生し得ます。

目次

[編集] ヘルパー変数テンプレート

template< class Rep >
inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<Rep>::value;
(C++17以上)

[編集] 特殊化

std::chrono::treat_as_floating_point はプログラム定義型に対して特殊化しても構いません。

[編集]

#include <iostream>
#include <chrono>
#include <thread>
 
void timed_piece_of_code() 
{
    std::chrono::milliseconds simulated_work(2);
    std::this_thread::sleep_for(simulated_work);
}
 
int main() 
{
    auto start = std::chrono::high_resolution_clock::now();
 
    std::cout << "Running some timed piece of code..." << '\n';
    timed_piece_of_code();
 
    auto stop = std::chrono::high_resolution_clock::now();
 
    // A floating point milliseconds type
    using FpMilliseconds = 
        std::chrono::duration<float, std::chrono::milliseconds::period>;
 
    static_assert(std::chrono::treat_as_floating_point<FpMilliseconds::rep>::value, 
                  "Rep required to be floating point");
 
    // Note that implicit conversion is not allowed here    
    auto i_ms = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start);
 
    // Note that implicit conversion is allowed here
    auto f_ms = FpMilliseconds(stop - start);
 
    std::cout << "Time in milliseconds, using default rep: "
              << i_ms.count() << '\n';
 
 
    std::cout << "Time in milliseconds, using floating point rep: "
              << f_ms.count() << '\n';
 
}

出力例:

Running some timed piece of code...
Timing stats:
  Time in milliseconds, using default rep: 2
  Time in milliseconds, using floating point rep: 2.57307

[編集] 関連項目