使函数接受可选选项以接受非可选选项吗? [英] Make a function accepting an optional to accept a non-optional?

查看:82
本文介绍了使函数接受可选选项以接受非可选选项吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在std::optional上以monad风格编写语法糖.请考虑:

I'm trying to write syntactic sugar, in a monad-style, over std::optional. Please consider:

template<class T>
void f(std::optional<T>)
{}

就这样,即使存在从Tstd::optional<T> <的转换,也不能使用非可选的T 1 (例如int)调用此函数. sup> 2 .

As is, this function cannot be called with a non-optional T1 (e.g. an int), even though there exists a conversion from T to std::optional<T>2.

是否有一种方法可以使f接受std::optional<T>T(在调用者站点上转换为可选),而无需定义重载 3 ?

Is there a way to make f accept an std::optional<T> or a T (converted to an optional at the caller site), without defining an overload3?

1) f(0):error: no matching function for call to 'f(int)'note: template argument deduction/substitution failed,(

1) f(0): error: no matching function for call to 'f(int)' and note: template argument deduction/substitution failed, (demo).
2) Because template argument deduction doesn't consider conversions.
3) Overloading is an acceptable solution for a unary function, but starts to be an annoyance when you have binary functions like operator+(optional, optional), and is a pain for ternary, 4-ary, etc. functions.

推荐答案

另一个版本.这不涉及任何内容:

Another version. This one doesn't involve anything:

template <typename T>
void f(T&& t) {
    std::optional opt = std::forward<T>(t);
}

类模板参数推论已经在这里做对了.如果toptional,将首选复制扣除对象,并且我们会得到相同的类型.否则,我们将其包装.

Class template argument deduction already does the right thing here. If t is an optional, the copy deduction candidate will be preferred and we get the same type back. Otherwise, we wrap it.

这篇关于使函数接受可选选项以接受非可选选项吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆