[英]How to properly take a boost::optional<std::chrono::duration> as a function parameter?
I need to have a function accept a boost::optional<std::chrono::duration>
. 我需要一个函数接受
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
. 我希望能够通过它,例如
std::chrono::milliseconds
, std::chrono::seconds
或boost::none
。
I have a couple of solutions which do not exactly achieve what I want. 我有几种解决方案无法完全实现我想要的功能。
Attempt 1: 尝试1:
template <class PeriodT>
void foo(boost::optional<std::chrono::duration<int64_t, PeriodT>> duration);
Problems with attempt 1: 尝试1的问题:
I cannot simply pass an std::chrono::duration
or even boost::none
. 我不能简单地传递
std::chrono::duration
甚至boost::none
。 To pass boost::none
I have to do the following: 要传递
boost::none
我必须执行以下操作:
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: 尝试2:
void foo(boost::optional<std::chrono::milliseconds> duration);
Problems with attempt 2: 尝试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
有什么办法可以让我的函数完全接受
boost::optional
of any rep/period? 任何代表/期间?
The function in my actual use case will need to accept multiple of these optional durations, so default values won't work for me. 在我的实际用例中,该函数将需要接受这些可选持续时间的倍数,因此默认值对我不起作用。
#include "boost/optional.hpp"
#include <chrono>
#include <iostream>
void foo(boost::optional<std::chrono::milliseconds> duration)
{
if (duration)
std::cout << duration->count() << "ms\n";
else
std::cout << "none\n";
}
void foo(std::chrono::milliseconds duration)
{
foo(boost::optional<std::chrono::milliseconds>{duration});
}
int
main()
{
using namespace std::chrono;
foo(10ms);
foo(10s);
foo(boost::none);
}
If you want to accept any chrono::duration<Rep, Period>
, you need to add one more overload and template everything. 如果要接受任何
chrono::duration<Rep, Period>
,则需要再添加一个重载并将所有内容模板化。 Also you need to decide what Rep
and Period
you want to default to for boost::none
: 另外,您还需要确定要提高默认值的
Rep
和Period
boost::none
:
#include "boost/optional.hpp"
#include "date/date.h"
#include <chrono>
#include <iostream>
template <class Rep, class Period>
void foo(boost::optional<std::chrono::duration<Rep, Period>> duration)
{
using date::operator<<;
if (duration)
std::cout << *duration << "\n";
else
std::cout << "none\n";
}
template <class Rep, class Period>
void foo(std::chrono::duration<Rep, Period> duration)
{
foo(boost::optional<std::chrono::duration<Rep, Period>>{duration});
}
void foo(boost::none_t)
{
foo(boost::optional<std::chrono::seconds>{});
}
int
main()
{
using namespace std::chrono;
foo(10ms);
foo(10s);
foo(boost::none);
}
Above I've also used Howard's date lib for easier printing of the arbitrary durations: 上面我还使用了霍华德的日期库来轻松打印任意持续时间:
10ms
10s
none
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.