Question

I want to write a very portable code, and I need to call std::chrono::system_clock::now() in the initialization. Can this function throw an exception ?

In c++11 standard, there is no mention of that issue. (At least, I didn't found one)

In Visual Studio, the sourcecode is as follow :

static time_point now() _NOEXCEPT
    {   // get current time
    return (time_point(duration(_Xtime_get_ticks())));
    }

With the _NOEXCEPT keyword, the answer in visual studio is pretty obvious. But what about the general case ?

Was it helpful?

Solution 2

Can this function throw an exception ?

No, it cannot.

Per paragraph 20.11.7/1 of the C++11 Standard, system_clock must satisfy the TrivialClock requirements.

Moreover, per paragraph 20.11.3/4, the now() member function of clocks that satisfy the TrivialClock requirement shall not throw:

A type TC meets the TrivialClock requirements if:

[...]

the function TC::now() does not throw exceptions, and

[...]

OTHER TIPS

From §20.11.7.1

class system_clock {
public:
  ...
    static time_point now() noexcept;  
  ...
};

So the standard states it cannot throw an exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top