1

How can I create an array from the parameter pack?

    template<typename T, typename... Tpack>
    void covert(Tpack ...pack){
        T *arr = new T[???]; //TODO: how to get Tpack size?
        // TODO: how to fill array?
    }
sergej.p
  • 85
  • 1
  • 5

1 Answers1

2

You might do:

template<typename T, typename... Tpack>
void covert(Tpack ...pack){
    T *arr = new T[sizeof...(Tpack)]{pack...};
    // ...
    delete[] arr;
}

Demo

but your function is strange as-is. std::tuple might be more appropriate, or change input parameter to std::initializer_list<T> or std::array<T, N>.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 1
    Mr. quick answer man.... it's not even clear what the OP wants to achieve... I mean, why not just do something like `int t[] = { 1, 2, 3, 4, 5 };` – JHBonarius Jun 10 '21 at 13:43
  • I want to call the function like this: convert (1, 2, 3, 4, 5) – sergej.p Jun 10 '21 at 13:50
  • 1
    @JHBonarius: We ask for MCVE, and OP provides it and question is answerable. That reduction might make the example with poor meaning, I agree. And so now we can speculate about what OP really wants. – Jarod42 Jun 10 '21 at 13:51