This is a follow-up question for A recursive_transform for std::vector with various return type. Thanks to G. Sliepen provide further review suggestions. After digging into the topic of achieving a more generic recursive_transform
function in both various output type and various container type, I still have no simple solution. However, based on G. Sliepen's answer, the case of the std::vector
, std::deque
and std::list
container types may be resolved. I am trying to implement additional another overload recursive_transform
function for std::array
. Here's my implementation.
template<class T, std::size_t S, class F>
auto recursive_transform(std::array<T, S>& input, const F& f)
{
using TransformedValueType = decltype(recursive_transform(*input.cbegin(), f));
std::array<TransformedValueType, S> output;
std::transform(input.cbegin(), input.cend(), output.begin(),
[f](auto& element)
{
return recursive_transform(element, f);
}
);
return output;
}
The test case of std::array
:
// std::array<int, 10> -> std::array<std::string, 10>
std::array<int, 10> test_array;
for (int i = 0; i < 10; i++)
{
test_array[i] = 1;
}
auto recursive_transform_result5 = recursive_transform(
test_array,
[](int x)->std::string { return std::to_string(x); }); // For testing
std::cout << "string: " + recursive_transform_result5.at(0) << std::endl;
Here's the Godbolt link. The code in this link including the test cases for std::vector
, std::deque
and std::list
.
All suggestions are welcome.
The summary information:
Which question it is a follow-up to?
A recursive_transform for std::vector with various return type
What changes has been made in the code since last question?
The handleable container type in the previous version of
recursive_transform
function isstd:vector
. With G. Sliepen's answer, this handleable container type list is extended tostd::vector
,std::deque
andstd::list
. Then, I am trying to deal withstd::array
here.Why a new review is being asked for?
In this version of
recursive_transform
function, it seems that working well in the mentioned test casestd::array<int, 10> -> std::array<std::string, 10>
. However, there is some issues when it comes to the more complex case likestd::array<std::array<int, 10>, 10> -> std::array<std::array<std::string, 10>, 10>
(the scalability to be improved!) If there is any suggestion or possible idea about this, please let me know.