What is the C++11 equivalent to boost::date_time::not_a_date_time?

前端 未结 2 1257
独厮守ぢ
独厮守ぢ 2021-01-06 18:03

I\'m modifying an old project, and at the same time I\'m updating several things to bring it up to C++11.

I\'d like to replace various uses of boost::date_time with

2条回答
  •  礼貌的吻别
    2021-01-06 18:14

    Given the fact that it exists as part of a group

    bool is_infinity() const
    bool is_neg_infinity() const
    bool is_pos_infinity() const
    bool is_not_a_date_time() const
    

    it is pretty clear that this is done by using a floating-point type for the internal representation and setting the value to a NaN (not-a-number).

    In std::chrono, the representation type is required to be an arithmetic type. Floating-point types therefore qualify, and you can use the same trick.

    Given a std::duration, you could then test it using

    std::isnan(dur.count())
    

    (Naturally, you should use a quiet NaN value, not a signalling NaN, so you don't trigger floating-point traps)

提交回复
热议问题