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);
pinki
  • 23
  • 5
  • What does "combine 3 functions into one" mean to you? – Sam Varshavchik Dec 15 '19 at 13:36
  • 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 '19 at 13:37
  • Looks like you want just one template function with a parameter pack, right?) – Sergey Kolesnik Dec 15 '19 at 13:38
  • so that she does the same thing but can still do size4, size5, etc. if I need more arrays. – pinki Dec 15 '19 at 13:39
  • 1
    @gsamaras , Sergey Kolesnik yes that's what i want – pinki Dec 15 '19 at 13:40
  • 1
    I suspect [something like this](https://ideone.com/ZuPJ0P) 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 '19 at 13:42
  • @WhozCraig It seems that this is what I need to test now. – pinki Dec 15 '19 at 13:47
  • 1
    @WhozCraig Thanks. This link is the answer to my question, the question is resolved. – pinki Dec 15 '19 at 13:50
  • 1
    @pinki I suspected that might be the case. Good luck. – WhozCraig Dec 15 '19 at 13:51
  • ```template auto get_nested_vector() { if constexpr (n) return get_nested_vector, n - 1>(); else return T(); } template using nest_vector = decltype(get_nested_vector()); static_assert(std::is_same_v, int>); static_assert(std::is_same_v, std::vector>); static_assert(std::is_same_v, std::vector>>);``` C++17 – Sergey Kolesnik Dec 15 '19 at 13:57

1 Answers1

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);
}
Mestkon
  • 3,532
  • 7
  • 18