Namensräume
Varianten
Aktionen

std::chrono::year::ok

Von cppreference.com
< cpp‎ | chrono‎ | year
 
 
 
 
constexpr bool ok() const noexcept;
(seit C++20)

Prüft, ob der gespeicherte Jahreswert in *this im gültigen Bereich liegt, d.h. [-3276732767].

[bearbeiten] Rückgabewert

true, wenn der gespeicherte Jahreswert in *this im Bereich [-3276732767] liegt. Andernfalls false.

[bearbeiten] Mögliche Implementierung

Siehe die Implementierungen in libstdc++, libc++ und Howard Hinants date.h.

class Year
{
    short year_;   // exposition-only
 
public:
 
    bool ok() const noexcept { return year_ != std::numeric_limits<short>::min(); }
 
    /*...*/
};

[bearbeiten] Beispiel

#include <chrono>
#include <iomanip>
#include <iostream>
 
int main()
{
    std::cout << "input year │ internal value │ ok()\n" << std::boolalpha;
 
    for (const int i : {2020, 0x8000, 0x8001, 0xFFFF, 0x18000})
    {
        const std::chrono::year y{i};
        std::cout << std::setw(10) << i << " │ "
                  << std::setw(14) << static_cast<int>(y) << " │ "
                  << y.ok() << '\n';
    }
}

Mögliche Ausgabe

input year │ internal value │ ok()
      2020 │           2020 │ true
     32768 │         -32768 │ false
     32769 │         -32767 │ true
     65535 │             -1 │ true
     98304 │         -32768 │ false