0

I need to combine everything, but I don’t know how even there is no hint how this can be done.

If this is not possible, write an answer so that I do not look for a way to implement this.

template <class T1, class T2, class T3>
auto vector_ptr(T1& size, T2& size2, T3& size3) {
    auto result = make_shared<vector<vector<vector<int64_t>>>>(size, vector<vector<int64_t>>(size2, vector<int64_t>(size3)));
    return *result;
}

template <class T1, class T2>
auto vector_ptr(T1& size, T2& size2) {
    auto result = make_shared<vector<vector<int64_t>>>(size, vector<int64_t>(size2));
    return *result;
}

template <class T1>
auto vector_ptr(T1& size) {
    auto result = make_shared<vector<int64_t>>(size);
    return *result;
}

main

auto v_arr3 = vector_ptr(size3, size2, size);

auto v_arr2 = vector_ptr(size2, size);

auto v_arr1 = vector_ptr(size);

and so on if necessary.

auto v_arr4 = vector_ptr(size4, size3, size2, size);
10
  • 1
    @SamVarshavchik I believe the OP wants a method with a dynamic number of parameters (and not fixed as in their implementation). Right pinki? If not, we can remove the duplicate.
    – gsamaras
    Dec 15, 2019 at 13:37
  • 1
    @gsamaras , Sergey Kolesnik yes that's what i want
    – pinki
    Dec 15, 2019 at 13:40
  • 1
    I suspect something like this was what you were looking for. Good luck. Sorry this was closed, as it's a pretty interesting task imho (though the title needs some serious clarification).
    – WhozCraig
    Dec 15, 2019 at 13:42
  • 1
    @WhozCraig Thanks. This link is the answer to my question, the question is resolved.
    – pinki
    Dec 15, 2019 at 13:50
  • 1
    @pinki I suspected that might be the case. Good luck.
    – WhozCraig
    Dec 15, 2019 at 13:51

1 Answer 1

1
#include <memory>
#include <type_traits>
#include <vector>

template<class T, class Head>
auto
make_vector(Head size)
{
    return std::vector<T>(size);
}

template<class T, class Head, class ...Args,
         std::enable_if_t<(sizeof...(Args) > 0), int> = 0>
auto
make_vector(Head size, Args... args)
{
    return std::vector(size, make_vector<T, Args...>(args...));
}

template<class T, class ...Args>
auto
vector_ptr(Args... args)
{
    using vector_type = decltype(make_vector<T>(args...));
    return std::make_shared<vector_type>(make_vector<T>(args...));
}

Requires c++17 for std::vector deduction guides though, can be avoided by using a decltype().

make_vector can be combined using if constexprinstead of SFINAE as such:

template<class T, class Head, class ...Args>
auto
make_vector(Head size, Args... args)
{
    if constexpr (sizeof...(Args) > 0)
        return std::vector(size, make_vector<T, Args...>(args...));
    else
        return std::vector<T>(size);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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