std::chrono::duration construction

296 views
Skip to first unread message

Bjorn Reese

unread,
Apr 24, 2016, 6:38:39 AM4/24/16
to std-dis...@isocpp.org
Does anybody know the rationale why the tick count of chrono::duration
is left uninitialized by default?

std::chrono::duration<int> d; // tick count is uninitialized

Vicente J. Botet Escriba

unread,
Apr 24, 2016, 7:52:34 AM4/24/16
to std-dis...@isocpp.org
Hi, I guess is to follow the pattern don't pay for what you don't use, but Howard would explain it better.

If you want the representation to be default initialized to zero, you can just provide one that do that


std::chrono::duration<MyInt> d; // MyInt default constructor initialize the value to zero.

Vicente

Howard Hinnant

unread,
Apr 24, 2016, 10:46:58 AM4/24/16
to std-dis...@isocpp.org
Vicente is correct. Because of the expected high use of durations, I did not want to impose the expense of always zero initializing them. However, just like scalars, they are easily zero-initialized:

std::chrono::duration<int> d; // tick count is uninitialized
std::chrono::duration<int> e{}; // tick count is zero-initialized

Howard

signature.asc
Reply all
Reply to author
Forward
0 new messages