I need to have a function accept a boost::optional<std::chrono::duration>
. I would like to be able to pass it, for example, either std::chrono::milliseconds
, std::chrono::seconds
, or boost::none
.
I have a couple of solutions which do not exactly achieve what I want.
Attempt 1:
template <class PeriodT> void foo(boost::optional<std::chrono::duration<int64_t, PeriodT>> duration);
Problems with attempt 1:
I cannot simply pass an std::chrono::duration
or even boost::none
. To pass boost::none
I have to do the following:
boost::optional<std::chrono::seconds> input = boost::none; foo(input);
I cannot call
foo(boost::none);
or
std::chrono::seconds input(10); foo(input);
Attempt 2:
void foo(boost::optional<std::chrono::milliseconds> duration);
Problems with attempt 2:
Automatic conversion won't happen.
foo(std::chrono::seconds(10));
Will not compile.
foo(std::chrono::milliseconds(10));
Will compile, and so will
foo(boost::none);
Is there any way I can have a function cleanly accept a boost::optional
of any rep/period?
Aucun commentaire:
Enregistrer un commentaire