2016-10-13 84 views
1
boost/std :: chrono::time_point my_time_point(/* invalid value */); 

我需要存儲無效/不符合/標記值。我怎麼可能做到這一點?std :: chrono :: time_point無效值

+4

查找到的boost ::可選或std: :可選:http://www.boost.org/doc/libs/1_61_0/libs/optional/doc/html/index.html –

+0

你確定它會有幫助嗎?我需要'my_time_point'來存儲無效值 – user3600124

+2

'可選'的要點是在您選擇的域中提供一個「額外值」,表示「空」/「無效」狀態 –

回答

1

您可以使用boost::optional(或std::optional,如果你有C++ 17的支持)代表的chrono::time_point無效狀態:

#include <chrono> 
#include <boost/optional.hpp> 

int main() 
{ 
    using my_clock = std::chrono::system_clock; 
    using my_time_point = std::chrono::time_point<my_clock>; 

    boost::optional<my_time_point> maybe_time_point; 

    // Set to invalid value: 
    maybe_time_point = boost::none; 

    // If 'maybe_time_point' is valid... 
    if(maybe_time_point) 
    { 
     auto inner_value = *maybe_time_point; 
     /* ... */ 
    } 
    else { /* ... */ } 
} 

(You can run it on wandbox.)

+0

Upvoted,但挑剔:OP詢問'std :: chrono :: time_point',而不是'boost :: chrono :: time_point'。 :-) –

+0

@HowardHinnant:固定! –

相關問題