5

In C++ I have this struct from C. This code is very old and cannot be modified:

struct Point {
   double coord[3];
};

On the other hand, I have this modern function which returns modern std::array instead of raw arrays:

std::array<double, 3> ComputePoint();

Currently, to initialize a Point from the returned value, I manually extract each element from the std::array:

std::array<double, 3> ansArray{ComputePoint()};
Point ans{ansArray[0], ansArray[1], ansArray[2]};

This solution is feasible because there are only three coordinates. Could I templatize it for a general length? I would like something similar to the opposite conversion: std::to_array.

2 Answers 2

6

You can use std::apply to do it for any length:

#include <array>
#include <tuple>

Point p = std::apply([](auto... args) {
    return Point{args...};
}, computePoint());

If you run into this pattern a lot, it could be worth making a convenience function template:

template <typename T, typename U>
T make_from_apply(U&& from) {
    return std::apply([]<typename... Args>(Args&&... args) {
        return T{std::forward<Args>(args)...};
    }, std::forward<U>(from));
}

Point p = make_from_apply<Point>(computePoint());

See live example at Compiler Explorer

All the std::forwarding isn't strictly necessary if Args is only going to be fundamental types like int and double. In that case, you can omit it.

1
1

Could I templatize it for a general length?

Yes, you can use std::index_sequence to generailze this for any length as shown below:

template<std::size_t N, typename T> constexpr auto make_array(const std::array<T, N> &ref)   
{
    return [&]<std::size_t... Indices>(std::index_sequence<Indices...>)
    {
        return Point{(ref[Indices])...};     
    }(std::make_index_sequence<N>());   
} 
int main()
{
    std::array<double, 3> ansArray{ComputePoint()};
    Point p = make_array(ansArray);       
}

Working demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.