名前空間
変種

std::chrono::round(std::chrono::duration)

提供: cppreference.com
 
 
ユーティリティライブラリ
汎用ユーティリティ
日付と時間
関数オブジェクト
書式化ライブラリ (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 スタイルの日付と時間
 
 
<tbody> </tbody>
ヘッダ <chrono> で定義
template <class ToDuration, class Rep, class Period> constexpr ToDuration round(const duration<Rep, Period>& d);
(C++17以上)

d に最も近い ToDuration で表現可能な値が返されます。 そのような値が2つある場合は、偶数の値 (つまり t % 2 == 0 であるような値 t) が返されます。

この関数は、 ToDurationstd::chrono::duration の特殊化でないか、 std::chrono::treat_as_floating_point<typename ToDuration::rep>::valuefalse でなければ、オーバーロード解決に参加しません。

引数

d - 変換する duration

戻り値

ToDuration 型の最も近い duration に丸められた d。 ちょうど中間の場合は偶数に丸められます。

実装例

template <class T> struct is_duration : std::false_type {};
template <class Rep, class Period> struct is_duration<
    std::chrono::duration<Rep, Period>> : std::true_type {};

template <class To, class Rep, class Period,
          class = std::enable_if_t<is_duration<To>{} &&
                 !std::chrono::treat_as_floating_point<typename To::rep>{}>>
constexpr To round(const std::chrono::duration<Rep, Period>& d)
{
    To t0 = std::chrono::duration::floor<To>(d);
    To t1 = t0 + To{1};
    auto diff0 = d - t0;
    auto diff1 = t1 - d;
    if (diff0 == diff1) {
        if (t0.count() & 1)
            return t1;
        return t0;
    } else if (diff0 < diff1) {
        return t0;
    }
    return t1;
}

関連項目

時間を異なる刻み幅を持つ別の時間に変換します
(関数テンプレート) [edit]
時間を別の時間に切り捨て変換します
(関数テンプレート) [edit]
時間を別の時間に切り上げ変換します
(関数テンプレート) [edit]
time_point を別の time_point の最も近い値に変換します
(関数テンプレート) [edit]
(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)(C++11)
最も近い整数を返します。 ちょうど中間の場合はゼロから離れる方向に丸めます
(関数) [edit]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.