Function Subspace :: sus :: construct :: into

template <class FromType>
auto into(FromType&& from) -> auto

Converts from the given value to whatever a receiver requires.

The result will be receivable if Into<FromType, ToType> is satisfied where ToType is deduced by the type constructed from the return value of into.

The value returned by into should be immediately converted into the desired type, and never held as an lvalue itself. The returned type holds a reference to the input that is used to construct the deduced ToType from it.

If the argument to into is Copy then it will be copied if it is an lvalue or const. If the argument to into is an rvalue, it will be moved when constructing the ToType.

Examples

The into function deduces the target type while performing an explicit conversion via the From concept (and thus a static from method on the target type).

auto f = [](Option<i32> i) { return i.unwrap_or(-1); };
auto num = 3_i32;
// Option<T> can be converted into from its inner type T.
sus_check(f(sus::into(num)) == 3);

The Into concept allows generic code to accept any input type that can be explicitly converted into the target type. The body of the function can use the into function to perform the conversion.

// f() accepts anything that can be converted to Option<i32> via into().
auto f = [](Into<Option<i32>> auto in) { return Option<i32>(sus::into(in)); };
auto num = 3_i32;
// num will be passed to Option<i32>::from() inside f().
sus_check(f(num).unwrap_or(-1) == 3);