Alright, I messed up asking this question the first time.
Is there a way, idiomatically, to provide a constructor which takes one or more std::optional<T> and returns a std::optional<U>? For instance, ideally I would love some kind of syntax like
#include <optional>
struct Rational {
explicit Rational(int i) : num{i}, denom{1} {}
Rational(int i, int j) : num{i}, denom{j} {}
// magic here?...
int num, denom;
};
int main()
{
Rational i1 = Rational(1); // the number 1
Rational h2 = Rational(1,2); // the number 1/2
std::optional<int> opt_i{}, opt_j{};
std::optional<Rational> e3 = std::optional<Rational>(opt_i);
// empty, converting copy constructor
/* std::optional<Rational> e4 = std::optional<Rational>(opt_i, 2); */
/* std::optional<Rational> e5 = std::optional<Rational>(2, opt_i); */
/* std::optional<Rational> e6 = std::optional<Rational>(opt_i, opt_j); */
// error, no constructor
// ideally would construct empty std::optional<Rational>
opt_i = 3;
std::optional<Rational> i7 = std::optional<Rational>(opt_i);
// contains Rational(3), the number 3
/* std::optional<Rational> r8 = std::optional<Rational>(opt_i, 2); */
/* std::optional<Rational> r9 = std::optional<Rational>(2, opt_i); */
// error, no constructor
// ideally would construct std::optional<Rational> containing 3/2 and 2/3, respectively
opt_j = 4;
/* std::optional<Rational> r10 = std::optional<Rational>(opt_i, opt_j); */
// error, no constructor
// ideally would construct std::optional<Rational> containing 3/4
}
Thanks to 463035818_is_not_an_ai's answer on the first question, we know that for simple converting/copy constructors std::optional provides an overload for its constructor. For general constructors, I believe this is not currently possible. What are my best options?