mirror of
https://github.com/isocpp/CppCoreGuidelines.git
synced 2024-03-22 13:30:58 +08:00
Merge pull request #285 from Eliyahu-Ravuna/patch-5
Feedback of Jared in #283 incorporated into I.4
This commit is contained in:
commit
27e9a95048
|
@ -1108,9 +1108,24 @@ In the following example, it is not clear from the interface what time_to_blink
|
|||
|
||||
std::chrono::duration types introduced in C++11 helps making the unit of time duration explicit.
|
||||
|
||||
void blink_led(milliseconds time_to_blink) //good - the unit is explicit
|
||||
{
|
||||
//...
|
||||
//do something with time_to_blink
|
||||
//...
|
||||
}
|
||||
|
||||
void use()
|
||||
{
|
||||
blink_led(1500ms);
|
||||
}
|
||||
|
||||
The function can also be written in such a way that it will accept any time duration unit.
|
||||
|
||||
template<class rep, class period>
|
||||
void blink_led(duration<rep, period> time_to_blink) //good - accepts any unit
|
||||
{
|
||||
//assuming that millisecond is the smallest relevant unit
|
||||
auto milliseconds_to_blink = duration_cast<milliseconds>(time_to_blink);
|
||||
//...
|
||||
//do something with milliseconds_to_blink
|
||||
|
|
Loading…
Reference in New Issue
Block a user