A recursive_transform for std::vector with various return type This is the follow-up question for A recursive_transform Function For Various Type Nested Iterable With std::variant Implementation in C++ and A get_from_variant function in C++. Thanks to G. Sliepen and Quuxplusone provide detailed review suggestions. However, the existed version of recursive_transform function assumes the return type is always the same as the input type. In other words, it works well with the lambda function like [](double x)-&gt;double { return x + 1; } (the type of both input and output are double) or [](int x)-&gt;int { return x + 1; } (the type of both input and output are int). In the next step, I want to focus on the case which the return type is different from the input type. For example, [](int x)-&gt;std::string { return std::to_string(x); }. Because the origin return type of recursive_transform is specified in T, it can not handle the case which the type of the processed output from the lambda function f is different from T. Let's change type T into auto as below. This auto syntax used here makes type deriving adaptive.
template&lt;class T, class F&gt;
auto recursive_transform(const T&amp; input, const F&amp; f) {
    return f(input);
}

The another part of this recursive_transform is the recursive structure and it is more complex than above. The container has been specified in std::vector here first.
template&lt;class T, class F&gt; requires is_iterable&lt;T&gt;
auto recursive_transform(const T&amp; input, const F&amp; f) {
    typedef typename std::iterator_traits&lt;typename T::iterator&gt;::value_type
        value_type;

    std::vector&lt;decltype(recursive_transform(std::declval&lt;value_type&amp;&amp;&gt;(), f))&gt; output(input.size());
    std::transform(input.begin(), input.end(), output.begin(),
        [f](auto&amp; element)
        {
            return recursive_transform(element, f);
        }
    );
    return output;
}

The test of the above template function recursive_transform.
std::vector&lt;int&gt; test_vector = {
    1, 2, 3
};
auto recursive_transform_result = recursive_transform(
    test_vector,
    [](int x)-&gt;std::string { return std::to_string(x); });                          //  For testing

std::cout &lt;&lt; &quot;string: &quot; + recursive_transform_result.at(0) &lt;&lt; std::endl;            //  recursive_transform_result.at(0) is a std::string

std::vector&lt;decltype(test_vector)&gt; test_vector2 = {
    test_vector, test_vector, test_vector
};

auto recursive_transform_result2 = recursive_transform(
    test_vector2,
    [](int x)-&gt;std::string { return std::to_string(x); });                          //  For testing

std::cout &lt;&lt; &quot;string: &quot; + recursive_transform_result2.at(0).at(0) &lt;&lt; std::endl;     // recursive_transform_result.at(0).at(0) is also a std::string

A Godbolt link is here.
All suggestions are welcome.

Which question it is a follow-up to?
A recursive_transform Function For Various Type Nested Iterable With std::variant Implementation in C++ and
A get_from_variant function in C++

What changes has been made in the code since last question?
In the previous version of recursive_transform function, it works well when the return type is as same as the input type. The main idea in this question is trying to implement a extended version which the return type is different from the input type.

Why a new review is being asked for?
The previous version recursive_transform function assumes the return type is always the same as the input type. I am trying to focus on the case which the return type is different from the input type to make the function more generic. However, I know that I make my algorithms more specialized in std::vector again in this version of code. I have no idea how to implement a more generic recursive_transform function in both various output type and various container type in a simple and smart way. If there is any suggestion or possible idea about this, please let me know.
I'm afraid this is reaching the limits of my knowledge about templates in C++, but I'll try to answer it anyway as best as I can:
Use std::back_inserter() to fill the vectors
Instead of constructing a vector of a given size, just declare an empty vector, but reserve enough capacity, and then use std::back_inserter() to fill it:
std::vector&lt;decltype(recursive_transform(std::declval&lt;value_type&amp;&amp;&gt;(), f))&gt; output;
output.reserve(input.size());

std::transform(input.begin(), input.end(), std::back_inserter(output),
    [f](auto&amp; element)
    {
        return recursive_transform(element, f);
    }
);

However, if you don't use std::vector but a different container type, reserve() and/or std::back_inserter() might not be appropriate.
Determining the container type
So ideally, we don't want to declare a std::vector, but rather the outer container of type T. You can use template template parameters to deconstruct templated types:
template&lt;template&lt;class&gt; class Container, class ValueType, class Function&gt;
requires is_iterable&lt;Container&lt;ValueType&gt;&gt;
auto recursive_transform(const Container&lt;ValueType&gt; &amp;input, const Function &amp;f)
{
    // You want to be able to write this:
    using TransformedValueType = decltype(recursive_transform(*input.begin(), f));
    Container&lt;TransformedValueType&gt; output;
    ...
}

Unfortunately, that doesn't work, at least not with Clang, because a std::vector actually has two template parameters, and other containers might have more or less template parameters. So the solution to that problem is to declare ValueType as a template parameter pack:
template&lt;template&lt;class...&gt; class Container, class Function, class Ts...&gt;
requires is_iterable&lt;Container&lt;Ts...&gt;&gt;
auto recursive_transform(const Container&lt;Ts...&gt; &amp;input, const Function &amp;f)
{
    using TransformedValueType = decltype(recursive_transform(*input.begin(), f));
    Container&lt;TransformedValueType&gt; output;
    ...
}

Although of course this doesn't forward the second template parameter. Putting everything so far together:
template&lt;typename T&gt;
concept is_iterable = requires(T x)
{
    *std::begin(x);
    std::end(x);
};

template&lt;class T, class Function&gt;
auto recursive_transform(const T &amp;input, const Function &amp;f)
{
    return f(input);
}

template&lt;template&lt;class...&gt; class Container, class Function, class... Ts&gt;
requires is_iterable&lt;Container&lt;Ts...&gt;&gt;
auto recursive_transform(const Container&lt;Ts...&gt; &amp;input, const Function &amp;f)
{
    using TransformedValueType = decltype(recursive_transform(*input.begin(), f));
    Container&lt;TransformedValueType&gt; output;

    std::transform(std::begin(input), std::end(input), std::back_inserter(output),
        [&amp;](auto &amp;element)
        {
            return recursive_transform(element, f);
        }
    );

    return output;
}